使用mod_rewrite.c在.htaccess文件中重写冲突

时间:2012-12-14 15:27:29

标签: apache .htaccess mod-rewrite url-rewriting

我正在使用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文件以避免冲突?

1 个答案:

答案 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>