如何使用htaccess从网址中删除GET参数?
除一页外,我的网站不使用任何GET参数。尽管如此,我可以看到Google设法使用GET参数为一堆页面建立了索引。这对于SEO(重复内容)来说不是很好...
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)...
因此,我正在尝试编辑.htaccess文件,以在所有带有GET参数的URL之间进行301重定向。没有GET参数(一个网址除外)。一些示例:
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 =>网址r.php应该保留GET参数
到目前为止,我正在尝试删除所有GET参数,但它不起作用。
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?
您无法使用 RewriteRule
匹配查询字符串。
You cannot match query string using RewriteRule
.
您可以使用此通用规则删除所有查询字符串,但具有 DOT
的请求除外:
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]