2012-12-14 22 views
1

我正在使用mod_rewrite替換乾淨的URL。到現在爲止,一切工作正常:使用mod_rewrite.c改寫.htaccess文件中的衝突

<IfModule mod_rewrite.c> 
    Options +FollowSymlinks 
    RewriteEngine On 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteRule (.*) application.php?request=$1 [L,QSA] 
</IfModule> 

但後來,我只是嘗試添加變換www.mysite.com網址mysite.com另一個規則,使用下列內容:

<IfModule mod_rewrite.c> 
    RewriteCond %{HTTPS} !=on 
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] 
    RewriteRule^http://%1%{REQUEST_URI} [R=301,L] 
</IfModule> 

結果:當我訪問mysite.com它很好,但是當訪問www.mysite.com時,URL變得醜陋mysite.com/application.php?request=

如何調整我的.htaccess文件,使其沒有衝突?

回答

2

您需要確保重定向規則爲之前的路由規則,否則應用路由規則,重寫引擎循環(將繼續循環,直到URI停止更改),然後發生重定向,在重寫了URI。

<IfModule mod_rewrite.c> 
    Options +FollowSymlinks 
    RewriteEngine On 

    RewriteCond %{HTTPS} !=on 
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] 
    RewriteRule^http://%1%{REQUEST_URI} [R=301,L] 

    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteRule (.*) application.php?request=$1 [L,QSA] 
</IfModule>