X Robots Tag noindex特定页面 [英] X Robots Tag noindex specific page
问题描述
我在我的网站www.domain / privacy-policy /上有一个隐私政策页面,我希望该索引与X Robots标记保持一致。我已经尝试了以下代码,但不匹配
I have a Privacy Policy page on my website www.domain/privacy-policy/ which I would like to noindex with the X Robots Tag. I have tried the following code but it does not match
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
## Redirect HTTP to HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^privacy-policy - [env=NOINDEXFOLLOW:true]
Header set X-Robots-Tag "noindex, follow" env=NOINDEXFOLLOW
</IfModule>
# END WordPress
问题已被编辑为包含完整的htaccess文件
Question has been edited to include full htaccess file for clarity.
推荐答案
www.domain / privacy-policy /
privacy-policy位于URL路径中,而不是查询字符串中,如您在您的指令。尝试在您的 .htaccess
文件顶部附近执行以下操作:
"privacy-policy" is in the URL-path, not the query string, as you have used in your directive. Try something like the following instead, near the top of your .htaccess
file:
RewriteEngine On
RewriteRule ^privacy-policy - [env=NOINDEXFOLLOW:true]
Header set X-Robots-Tag "noindex, follow" env=NOINDEXFOLLOW
但是,最好使用mod_setenvif而不是mod_rewrite来设置环境变量:
However, it would be preferable to use mod_setenvif instead of mod_rewrite to set the environment variable:
SetEnvIf Request_URI "^/privacy-policy" NOINDEXFOLLOW
更新:由于您使用的是前控制器(WordPress指令),因此 RewriteRule
指令设置环境变量将需要放在WP指令之前的 .htaccess
文件的顶部。通过将此指令放置在WP指令之后,就不会对其进行处理。 (如果需要,可以在文件的后面显示 SetEnvIf
和 Header
指令。)
UPDATE: Since you are using a front-controller (WordPress directives), the RewriteRule
directive to set the environment variable would need to go at the top of your .htaccess
file, before the WP directives. By positioning this directive after the WP directives it simply does not get processed. (The SetEnvIf
and Header
directives can appear later in the file if you wish.)
但是,由于您正在使用前控制器并将所有请求重写为 index.php
,因此<$您看到的请求中未设置c $ c> NOINDEXFOLLOW 变量。重写为 index.php
后,Apache将其更改为 REDIRECT_NOINDEXFOLLOW
( REDIRECT _
前缀),这就是您需要在 Header
指令中检查的内容。因此,总而言之:
However, since you are using a front-controller and rewriting all requests to index.php
, the NOINDEXFOLLOW
variable is not being set in the request you are seeing. After the rewrite to index.php
Apache changes this to REDIRECT_NOINDEXFOLLOW
(REDIRECT_
prefix) and this is what you need to check for in the Header
directive. So, in summary:
SetEnvIf Request_URI "^/privacy-policy" NOINDEXFOLLOW
Header set X-Robots-Tag "noindex, follow" env=REDIRECT_NOINDEXFOLLOW
(不是很直观。)
如果使用 RewriteRule
指令代替设置 NOINDEXFOLLOW
环境变量,则它必须出现在文件的开头。
And if you use the RewriteRule
directive instead to set the NOINDEXFOLLOW
environment variable then this must appear at the start of the file.
这篇关于X Robots Tag noindex特定页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!