删除查询字符串和参数

Remove query string and parameters

由于一些不值得在这里讨论的原因,Google 一直在用 URL 中不必要的查询字符串索引我的一个网站,这些字符串是 wordfence_lhhidwordfence_logHuman。我想修改我的 .htaccess 文件以删除所有这些查询字符串。

我的URLs

example.com/page/111/?wordfence_lh=1&hid=CA2BA660BEFF26B9A17F8F85D7391BD4

example.com/page/80/?wordfence_logHuman=1&hid=647700EBF43600E7BC54103256F1D71B

预计 URL秒

example.com/page/111/

example.com/page/80/

我找到了删除单个参数的方法,但我仍然找不到删除多个查询参数的正则表达式或其他东西。 非常感谢任何帮助,非常感谢!

这是我的 .htaccess 文件的一部分:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} on [OR]
RewriteCond %{SERVER_PORT} ^555$ [OR]
RewriteCond %{HTTP:X-Forwarded-Proto} https
RewriteRule .* - [E=WPR_SSL:-https]
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteRule .* - [E=WPR_ENC:_gzip]
RewriteCond %{REQUEST_METHOD} GET
RewriteCond %{QUERY_STRING} =""
RewriteCond %{HTTP:Cookie} !(wordpress_logged_in_.+|wp-postpass_|wptouch_switch_toggle|comment_author_|comment_author_email_) [NC]
RewriteCond %{REQUEST_URI} !^(/(.+/)?feed/?.+/?|/(?:.+/)?embed/|/(index\.php/)?wp\-json(/.*|$)|/cantonicalt/)$ [NC]
RewriteCond %{HTTP_USER_AGENT} !^(facebookexternalhit).* [NC]
RewriteCond "%{DOCUMENT_ROOT}/wp-content/cache/wp-rocket/%{HTTP_HOST}%{REQUEST_URI}/index%{ENV:WPR_SSL}%{ENV:WPR_WEBP}.html%{ENV:WPR_ENC}" -f
RewriteRule .* "/wp-content/cache/wp-rocket/%{HTTP_HOST}%{REQUEST_URI}/index%{ENV:WPR_SSL}%{ENV:WPR_WEBP}.html%{ENV:WPR_ENC}" [L]
</IfModule>

这是简单的 SEO。您需要在 robots.txt 中阻止 GET 请求 只需将其添加到 robots.txt

Disallow: /?

并且所有 GET url 都是索引块(是无索引)。 Google 从搜索中删除回收站页面需要一些时间。

您可以阻止任何垃圾页面,只需添加

Disallow: *?s=
Disallow: *&s=

I did not see any other than 3 URL parameters wordfence_lh, hid and wordfence_logHuman. I want to remove them

如果您在任何其他 URL 上没有任何其他 URL 参数,那么最简单的方法是在存在任何查询字符串的情况下删除整个查询字符串。例如:

# Remove any query string on all URLs
RewriteCond %{QUERY_STRING} .
RewriteRule ^ %{REQUEST_URI} [QSD,R=301,L]

这需要放在 .htaccess 文件的顶部, 您现有的指令之前。

RewriteCond 指令检查是否存在任何查询字符串。 QSD 标志从重定向响应中丢弃查询字符串。

但是,如果您在 other URL 上还有其他 URL 参数需要保留,然后检查这些特定的 URL 参数(如第一个建议的那样),然后如果存在这些 URL 参数中的任何一个,则删除整个查询字符串。例如:

# Remove the entire query string if any one of the URL params are present
RewriteCond %{QUERY_STRING} (&|^)(wordfence_lh|hid|wordfence_logHuman)=
RewriteRule ^ %{REQUEST_URI} [QSD,R=301,L]

But I still don't want to interfere with other measurement tools like google analytics.

这不是问题,除非您在其他 URL 上使用 URL 参数并且这些参数有时会与您要删除的 URL 参数混合使用?


更新:

Recently I have just tested with... Is it the same with your 2nd code? What is the difference?

RewriteCond %{QUERY_STRING} ^(.*)&?wordfence_lh=[^&]+&?(.*)$ [NC]
RewriteRule ^/?(.*)$ /?%1%2 [R=301,L]

RewriteCond %{QUERY_STRING} ^(.*)&?wordfence_logHuman=[^&]+&?(.*)$ [NC]
RewriteRule ^/?(.*)$ /?%1%2 [R=301,L]

RewriteCond %{QUERY_STRING} ^(.*)&?hid=[^&]+&?(.*)$ [NC]
RewriteRule ^/?(.*)$ /?%1%2 [R=301,L]

不,这不是“相同”。 “试图”保留与要删除的 URL 参数混合的 URL 参数(如我上面最后一句话中所述)——这似乎不是您的要求。

但是,这些指令存在一些问题:

  1. 匹配太多,可能会损坏查询字符串。例如,它不仅匹配 hid=,它还会匹配 foohid=,然后将保留可能“破坏”查询字符串的 foo 部分。例如。给定一个像 foohid=123&bar=1 这样的查询字符串,上面的指令将重定向到 foobar=1,这显然是不正确的。

  2. 这一系列的 3 条规则可能会触发 3 次外部重定向,因为每次出现要删除的 URL 参数都会触发单独的重定向。这应该(并且可以)避免。在您的示例 URL 中(仅包含其中两个 URL 参数),您将获得两个重定向。两个重定向不一定太糟糕,但是,它可以减少到一个重定向(最坏的情况)。