htAccess 重写规则添加了不需要的 get 参数



我的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但这给出了相同的结果。

您需要

在内部路由规则之前保留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 缓存。

如果重定向 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]

最新更新