大网数据广告活动
网站运营与故障问答

How to correctly remove MULTIPLE parameters from the Google index using htaccess and canonical / noindex?

· 52CE 我爱测

I found a similar question with a great answer for removing a single URL parameter. However, what if I have 20+ URL parameters that I don't want indexed? Also, in the example solution below, it is assuming that you want to specif…

问题原文

原帖链接 · Bryan Earl · CC BY-SA 3.0

I found a similar question with a great answer for removing a single URL parameter. However, what if I have 20+ URL parameters that I don't want indexed?

Also, in the example solution below, it is assuming that you want to specify a parameter range (ex: ?id=0 to ?id=9)... In my situation I would simply want to not index anything with the ?id parameter at all, regardless of what follows in the url string. Let's also say that I would not want to index the ?start and ?Page parameters either... Can someone help me out with a revised version of the following code?

For NOINDEX:

<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule .* - [E=NOINDEX_HEADER:1] 
</IfModule>

<IfModule mod_headers.c>
Header set X-Robots-Tag "noindex, follow" env=NOINDEX_HEADER
</IfModule>

For CANONICAL:

<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule .* - [E=CANONICAL_HEADER:1]
</IfModule>

<IfModule mod_headers.c>
Header set Link '%{HTTP_HOST}%{REQUEST_URI}e; rel="canonical"' env=CANONICAL_HEADER
</IfModule>

Thank you @Evgeniy and @JohnMueller for the above code.
Reference: How to correctly remove parameters from the Google index?

已采纳解答

原帖链接 · MrWhite · CC BY-SA 3.0

You only really need to set the rel="canonical" header. This should be sufficient in ensuring only the canonical URL (ie. the one with no URL params) appears in the SERPs. Setting a noindex robots meta tag for such URLs would seem to be overkill (and a tad risky) IMO.

Presumably you are unable to set a rel="canonical" meta tag in the HTML itself?

...what if I have 20+ URL parameters that I don't want indexed?

Is it safe to say that you don't want any URL with any URL params (ie. any query string) indexed? In which case you can simply change your RewriteCond directive to read:

RewriteCond %{QUERY_STRING} .

That is, there is a query string of any length.

If, however, you want to exclude 20 specific URL params then you are going to have to name every one of them. For example:

RewriteCond %{QUERY_STRING} (?:^|&)(id|start|page|another)=

The (?:^|&) is a non-capturing group to ensure we only match these specific param names and not something like sid or lastpage, etc. (if they could possibly be URL param names).

Header set Link '%{HTTP_HOST}%{REQUEST_URI}e; rel="canonical"' env=CANONICAL_HEADER

This is invalid. You are missing the scheme (eg. http), e symbol after the %{HTTP_HOST} variable (this would result in a 500 error) and angled brackets (<..>) around the URL. This should be of the form:

Header set Link '<http://%{HTTP_HOST}e%{REQUEST_URI}e>; rel="canonical"'

Reference: (Google's support doc regarding canonical URLs)
https://support.google.com/webmasters/answer/139066?hl=en

UPDATE: However, the %{REQUEST_URI}e environment variable, when used in this context, includes the query string - which really defeats the object of this excercise. This whole block should be rewritten as:

RewriteCond %{QUERY_STRING} .
RewriteRule (.*) - [E=CANONICAL_URI:$1]
Header set Link '<http://%{HTTP_HOST}e/%{CANONICAL_URI}e>; rel="canonical"' env=CANONICAL_URI

Instead of using the REQUEST_URI variable, we capture the URL-path only (which excludes the query string) using the RewriteRule directive and store this in the CANONICAL_URI variable. This is then used in the Header directive instead.

There is also no need for the <IfModule> containers here. It either works or it breaks, these directives are not intended to be optional (are they?).

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2016-09-06)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。