How to remove GET parameters from url with htaccess?

喜夏-厌秋 提交于 2020-01-11 13:30:49

问题


My website do not use any GET parameters except on one page. Nonetheless, I can see that Google managed to index a bunch of my pages with GET parameters. This is not great for SEO (duplicate content)...

So I'm trying to edit my .htaccess to do 301 redirects between all urls with GET parameters to url without GET parameters (except for one url). Some examples:

  • example.com/?foo=42 => example.com/
  • example.com/about?bar=42 => example.com/about
  • example.com/r.php?foobar=42 => the url r.php should keep the GET parameters

So far I'm trying to remove all GET parameters, and it doesn't work.

RewriteEngine On 

RewriteRule ^(.*)\?(.*)$ http://www.example.com/$1 [L,NC,R=301]

Any idea how to fix that?


回答1:


You cannot match query string using RewriteRule.

You can use this generic rule to remove all query string except for requests that have DOT:

RewriteEngine On

RewriteCond %{QUERY_STRING} .
RewriteRule ^([^.]*)$ /$1? [L,NE,R=301]


来源:https://stackoverflow.com/questions/36848157/how-to-remove-get-parameters-from-url-with-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!