配置.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
RewriteCondlines check if.default.txtexists, then read its first line (ignoring extra newlines) into theAPP_VERSIONenvironment 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.txtdirectly if needed). - The final
RewriteRuletakes any request toapps/app1/xyzand internally forwards it toapps/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
RewriteCondcaptures the app name (e.g.,app1) from URLs starting with/apps/. - The next two conditions validate that the app has a
.default.txtfile and read its version intoAPP_VERSION. - The rewrite rule then maps
apps/app1/xyztoapps/app1/{VERSION}/xyzfor any non-existent file/directory request.
Important Notes:
- Permissions: Ensure the
.default.txtfiles have permissions that let Apache read them (usuallychmod 644works). - mod_rewrite: Double-check that mod_rewrite is enabled in your Apache configuration. If not, uncomment
LoadModule rewrite_module modules/mod_rewrite.soand 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.txtonly contains the version string (e.g.,v1.0) with no extra spaces or newlines—otherwise the regex might capture unwanted characters.
内容的提问来源于stack exchange,提问作者sirlouis
相关产品推荐
相关产品推荐

