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

时间:2016-04-09 14:24:04

标签: php wordpress .htaccess redirect

晚安....

我的网站位于 wordpress ,并且我不希望查询字符串网址在Google搜索结果中进行索引,因此对于此我使用下面的代码来显示主页网址

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

RewriteCond %{THE_REQUEST} \?
RewriteCond %{QUERY_STRING} =""
RewriteRule .? %{REQUEST_URI}? [R=301,L]

通过使用上面的代码,url会自动重定向到主网址...

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

重定向...

http://example.com

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

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

应该重定向..

分别为

http://example.com/folder/http://example.com/folder/ .....

任何人都可以帮助我纠正上面的代码..

感谢

1 个答案:

答案 0 :(得分:0)

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

RewriteEngine on


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

您的重定向无效的原因是您正在检查空查询字符串。

RewriteCond %{QUERY_STRING} =""

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

RewriteCond %{QUERY_STRING} .+