如何使用htaccess从网址中删除GET参数? [英] How to remove GET parameters from url with htaccess?

查看:154
本文介绍了如何使用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]

这篇关于如何使用htaccess从网址中删除GET参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆