问题
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