Conditional URL rewrite配置问题:多目录无法正确映射至对应dist目录
/app and /staging Prefixes Looks like your current rewrite rules aren’t distinguishing between the /app and /staging prefixes, which is why requests to example.com/staging are being routed to /app/dist/ instead of their own dist folder. Here’s how to fix this:
Corrected Apache Rewrite Configuration
RewriteEngine On # Handle requests to /staging (route to staging/dist) RewriteRule ^staging/$ /staging/dist/ [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !^/staging/dist/ RewriteRule ^staging/(.*)$ /staging/dist/$1 [L] # Handle requests to /app (route to app/dist) RewriteRule ^app/$ /app/dist/ [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !^/app/dist/ RewriteRule ^app/(.*)$ /app/dist/$1 [L] # Optional: Keep root URL pointing to app/dist if needed # RewriteRule ^$ /app/dist/ [L]
Why Your Original Rules Failed
Your original configuration had a broad final rewrite rule (^(.*)$) that matched all requests not pointing to an existing file/directory and not starting with /dist/. When someone visited example.com/staging, since /staging isn’t a file or directory, the rule captured staging as $1 and routed it to /app/dist/staging—that’s why you saw the wrong content.
By splitting the rules into separate blocks for /staging and /app, we:
- Target only requests starting with each specific prefix
- Use the
[L]flag to stop processing further rules once a match is found - Exclude requests already pointing to the respective
distfolders to prevent infinite loops
If you need the root URL (example.com/) to still point to /app/dist/, uncomment the optional root rule at the bottom—just make sure it comes after the staging/app rules so it doesn’t override them.
内容的提问来源于stack exchange,提问作者Silver Ringvee

