2011-07-20 15 views
0

我們有一個Rails應用,它處理來自m.host.comhost.com的請求。如果請求進入m.host.com,rails應用程序頁面緩存目錄是/public/cache/m/,如果請求來到host.com,則頁面緩存目錄爲/public/cache/www/如何通過Apache將訪問者重定向到基於子域的唯一頁面緩存目錄

問題是,第一個RewriteCond正在匹配m.host.comhost.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] 

回答

2

試試這個:

# 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,這就是爲什麼您的重寫對於任何請求都是相同的,解析器會將第二個RewriteRules應用於請求。

+0

謝謝,我不得不添加一個規則,使其全部工作:'重寫規則^/$ /索引[QSA]'。這樣當它運行你建議的規則時它會找到'index.html'。 –

相關問題