我正在使用mod_rewrite来获取干净的URL。到目前为止,一切正常:
<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
时,该网址变得丑陋mysite.com/application.php?request=
如何调整我的.htaccess文件以避免冲突?
答案 0 :(得分: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>