You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

配置.htaccess实现指向.default.txt指定的默认目录

Got it, let's tackle this .htaccess setup for your versioned app structure. Here's how to make Apache automatically route requests like example.com/apps/app1 to the version specified in .default.txt (e.g., v1.0):

Option 1: Single App (apps/app1 only)

If you only need this for one specific app, create an .htaccess file directly inside the apps/app1 directory with these rules:

RewriteEngine On

# Read the version from .default.txt and store it in an environment variable
RewriteCond %{DOCUMENT_ROOT}/apps/app1/.default.txt -f
RewriteCond %{DOCUMENT_ROOT}/apps/app1/.default.txt <([^\n]+)>
RewriteRule ^ - [E=APP_VERSION:%1]

# Route requests to the versioned directory if the requested path doesn't exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ %{ENV:APP_VERSION}/$1 [L]

How this works:

  • The first two RewriteCond lines check if .default.txt exists, then read its first line (ignoring extra newlines) into the APP_VERSION environment variable.
  • The next two conditions skip the rewrite if the requested file/directory already exists (so you can still access things like apps/app1/robots.txt directly if needed).
  • The final RewriteRule takes any request to apps/app1/xyz and internally forwards it to apps/app1/{VERSION}/xyz (where {VERSION} is whatever's in .default.txt).

Option 2: Multiple Apps (apps/app1, apps/app2, etc.)

If you have multiple apps each with their own .default.txt, use this root .htaccess (placed in your website's document root) to handle all of them:

RewriteEngine On

# Capture the app name from the URL and read its .default.txt version
RewriteCond %{REQUEST_URI} ^/apps/([^/]+)/?
RewriteCond %{DOCUMENT_ROOT}/apps/%1/.default.txt -f
RewriteCond %{DOCUMENT_ROOT}/apps/%1/.default.txt <([^\n]+)>
RewriteRule ^ - [E=APP_VERSION:%2]

# Route requests to the correct versioned directory for each app
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^apps/([^/]+)/(.*)$ apps/$1/%{ENV:APP_VERSION}/$2 [L]

How this works:

  • The first RewriteCond captures the app name (e.g., app1) from URLs starting with /apps/.
  • The next two conditions validate that the app has a .default.txt file and read its version into APP_VERSION.
  • The rewrite rule then maps apps/app1/xyz to apps/app1/{VERSION}/xyz for any non-existent file/directory request.

Important Notes:

  • Permissions: Ensure the .default.txt files have permissions that let Apache read them (usually chmod 644 works).
  • mod_rewrite: Double-check that mod_rewrite is enabled in your Apache configuration. If not, uncomment LoadModule rewrite_module modules/mod_rewrite.so and restart Apache.
  • External Redirect (Optional): If you want the browser address bar to show the versioned path (instead of internal forwarding), replace the [L] flag with [R=302,L] for testing, then switch to [R=301,L] once you're sure it works (301 is permanent redirect).
  • Clean .default.txt: Make sure .default.txt only contains the version string (e.g., v1.0) with no extra spaces or newlines—otherwise the regex might capture unwanted characters.

内容的提问来源于stack exchange,提问作者sirlouis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:59:34