如何使用htaccess重定向子文件夹查询字符串url


how to redirect sub folder query string url using htaccess

晚上好。。。。

我在wordpress中有我的网站,我不想在谷歌搜索结果中索引查询字符串url,所以为此我使用了下面的代码作为主页url,比如这个

RewriteCond %{QUERY_STRING} .
RewriteRule ^$ /? [R=301,L]
RewriteCond %{THE_REQUEST} '?
RewriteCond %{QUERY_STRING} =""
RewriteRule .? %{REQUEST_URI}? [R=301,L]

通过使用上面的代码在url下面自动重定向到主页url。。。

http://example.com/?或http://example.com/?paramater=1

重定向到…

http://example.com

但我担心的是,上面的代码不适用于子文件夹或子目录,如。。

http://example.com/folder/?paramater=1或http://example.com/folder/?

应该重定向到..

http://example.com/folder/或http://example.com/folder/分别地

有人能帮我纠正上面的代码吗。。

感谢

您可以使用以下代码从url中删除查询字符串:

RewriteEngine on

RewriteCond %{QUERY_STRING} .+
RewriteRule ^ %{REQUEST_URI}? [L,R=301]

重定向不起作用的原因是您正在检查空的查询字符串。

RewriteCond %{QUERY_STRING} =""

您需要检查查询字符串是否存在

RewriteCond %{QUERY_STRING} .+