Apache非www到www严重重写URL上的数组

时间:2013-09-25 22:12:34

标签: regex apache .htaccess mod-rewrite

我们有以下.htaccess配置:

RewriteEngine on
allow from all

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f

# otherwise forward it to index.php
RewriteRule . index.php

RewriteCond %{HTTP_HOST} ^domain1\.domain2\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain1\.domain2\.com$
RewriteRule ^(.*)$ "http\:\/\/www\.domain1\.com\/$1" [R=301,L]

RewriteCond %{HTTP_HOST} ^domain1.com$
RewriteRule ^/?$ "http\:\/\/www\.domain1\.com\/" [R=301,L]

在其他事情之间,我们希望将所有非www网址重定向到包含www。

的网址

除了URL如下所示的网页外,一切正常:

index?Form%5bplace%5d=Caribbean&Form%5bdestination%5d=Virgin+Islands&Form%5btype%5d=A

当我们输入不带www的网址时,我们的重定向最终会显示以下网址:

index?Form%255bplace%255d=Caribbean&Form%255bdestination%255d=Virgin+Islands&Form%255btype%255d=A

由于无法识别,因此会出现404错误。

知道如何避免这种情况吗?

2 个答案:

答案 0 :(得分:0)

用以下代码替换您的代码:

allow from all
Options +FollowSymLinks -MultiViews

# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# if a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
# otherwise forward it to index.php
RewriteRule . index.php [L]

RewriteCond %{HTTP_HOST} ^domain1\.domain2\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain1\.domain2\.com$
RewriteRule ^ http://www.domain1.com%{REQUEST_URI} [NE,R=301,L]

RewriteCond %{HTTP_HOST} ^domain1\.com$
RewriteRule ^/?$ http://www.domain1.com/ [R=301,L]

答案 1 :(得分:0)

正在发生的事情是%符号被转义为%25

You can avoid this using the NE flag on your rules