如何使用htaccess从url中删除GET参数? [英] How to remove GET parameters from url with htaccess?
问题描述
我的网站除了在一页之外不使用任何 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 到没有 GET 参数的 url 之间进行 301 重定向(一个 url 除外).一些例子:
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 => url 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]
这篇关于如何使用htaccess从url中删除GET参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!