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

Apache htaccess重写规则添加了不需要的get参数,apache,.htaccess,redirect,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/

我的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中

还尝试重定向301/城市/伦敦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]