htaccess rewriterule添加了不需要的get参数

时间:2015-01-12 20:50:26

标签: apache .htaccess redirect

我的htaccess文件中包含的内容:

RewriteEngine on

# --- Remove index.php from URLs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule .* index.php/$0 [PT,L]
RewriteRule ^(.*)$ index.php?/$1 [L]

RewriteRule ^city/([^/]+)/?$ /uk/city/$1 [R=301,L]

我喜欢它将example.com/city/london重定向到example.com/uk/city/london

奇怪的是它现在重定向到example.com/uk/city/london?city/london所以它似乎添加了将其作为get参数重定向到新URL所需的部分。

还尝试Redirect 301 /city/london http://www.example.com/uk/city/london,但这会得到相同的结果。

2 个答案:

答案 0 :(得分:0)

您需要在内部路由规则之前保留R=301(重定向)规则:

RewriteEngine on

RewriteRule ^city/([^/]+)/?$ /uk/city/$1 [R=301,L]

# --- Remove index.php from URLs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule .* index.php/$0 [PT,L]
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

同时清除浏览器缓存以对其进行测试,以避免命中旧的301缓存。

答案 1 :(得分:0)

如果重定向301不起作用,您可以尝试这样的事情:

RewriteRule /city/london uk/city/london [L]

以下是您使用其他已定义的.htaccess规则的示例:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteRule /city/london uk/city/london [L]

# --- Remove index.php from URLs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule .* index.php/$0 [PT,L]
RewriteRule ^(.*)$ index.php?/$1 [L]