如何通过Apache将访问者重定向到基于子域的唯一页面缓存目录

如何通过Apache将访问者重定向到基于子域的唯一页面缓存目录,apache,.htaccess,mod-rewrite,Apache,.htaccess,Mod Rewrite,我们有一个rails应用程序,它处理来自m.host.com和host.com的请求。如果请求进入m.host.com,rails应用程序页面缓存目录为/public/cache/m/,如果请求进入host.com页面缓存目录为/public/cache/www/ 问题是,第一个RewriteCond正在匹配到m.host.com和host.com的两个请求 # if the `HTTP_HOST` starts with "m." (m.host.com), look for the cach

我们有一个rails应用程序,它处理来自
m.host.com
host.com
的请求。如果请求进入
m.host.com
,rails应用程序页面缓存目录为
/public/cache/m/
,如果请求进入
host.com
页面缓存目录为
/public/cache/www/

问题是,第一个
RewriteCond
正在匹配到
m.host.com
host.com
的两个请求

# 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 [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]
尝试一下:

# 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]
通过将“L”添加到参数中,可以告诉解析器当前重写规则是应用于当前请求的最后一条规则


另外,RewriteCond只适用于单个下一个重写规则,这就是为什么您对任何请求的重写都是相同的,解析器将对请求应用两个第二个重写规则。

谢谢,我必须再添加一个规则才能使其正常工作:
RewriteRule^/$/index[QSA]
。这样,它将在以后运行您建议的规则时找到
index.html