htaccess rewriterule 添加不需要的 get 参数

htaccess rewriterule adds unwanted get parameter

我的 htaccess 文件中的内容:

RewriteEngine on

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

RewriteRule ^city/([^/]+)/?$ /uk/city/ [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/ [R=301,L]

# --- Remove index.php from URLs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule .* index.php/[=10=] [PT,L]
RewriteRule ^(.*)$ index.php?/ [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/ [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/[=11=] [PT,L]
RewriteRule ^(.*)$ index.php?/ [L]