.htaccess自定义使用参数将非“www”重定向到“www”

时间:2012-12-04 22:50:25

标签: .htaccess redirect

我需要使用HTTP,HTTPS和SSL来完成这项工作,这是我的实际(工作)重写:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
</IfModule>

但是我需要强制将非“www”网址(如http://domain.com)重定向到带有“www”的相同网址,并且仍在使用index.php?url = $ 1,因为我需要这个用于我的“路由器”引擎。

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

这应该做(在任何其他重写规则之前):

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{HTTPS} !=on
   RewriteCond %{HTTP_HOST} !^www.*$
   RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
   RewriteCond %{HTTPS} =on
   RewriteCond %{HTTP_HOST} !^www.*$
   RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=301]
   RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
</IfModule>

答案 1 :(得分:0)

只需在开头添加非www到www规则,如下所示:

#non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com/?$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
</IfModule>
相关问题