How to redirect visitors to unique page cache directories based on subdomain via Apache
We have a rails app which is handling requests from both m.host.com
and host.com
. If the request comes in to m.host.com
, the rails app page cache dir is /public/cache/m/
, and if the request comes to host.com
the page cache dir is /public/cache/www/
.
The problem is that the first RewriteCond
is being matched for both requests to m.host.com
and host.com
.
# if the `HTTP_HOST` starts with "m." (m.host.com), look for the cache in /cache/m/...
RewriteCond %{HTT开发者_StackOverflowP_HOST} ^m\..*
RewriteRule ^([^.]+)/$ /cache/m/$1.html [QSA]
RewriteRule ^([^.]+)$ /cache/m/$1.html [QSA]
# if not, look for the cache in /cache/www/...
RewriteCond %{HTTP_HOST} !^m\..*
RewriteRule ^([^.]+)/$ /cache/www/$1.html [QSA]
RewriteRule ^([^.]+)$ /cache/www/$1.html [QSA]
Give this a try:
# if the `HTTP_HOST` starts with "m." (m.host.com), look for the cache in /cache/m/...
RewriteCond %{HTTP_HOST} ^m\.
RewriteRule ^([^.]+)/?$ /cache/m/$1.html [L,QSA]
# if not, look for the cache in /cache/www/...
RewriteCond %{HTTP_HOST} !^m\.
RewriteRule ^([^.]+)/?$ /cache/www/$1.html [L,QSA]
By adding the 'L' to the parameters you tell the parser that the current RewriteRule is the last one applied to the current request.
Also a RewriteCond only applies to the single next RewriteRule, that's why your rewrites were the same for any request, the parser would apply both second RewriteRules to the request.
精彩评论