X Robots Tag noindex specific page

耗尽温柔 提交于 2019-12-24 08:46:17

问题


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

Question has been edited to include full htaccess file for clarity.


回答1:


www.domain/privacy-policy/

"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

However, it would be preferable to use mod_setenvif instead of mod_rewrite to set the environment variable:

SetEnvIf Request_URI "^/privacy-policy" NOINDEXFOLLOW

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.)

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

(Not quite so intuitive.)

And if you use the RewriteRule directive instead to set the NOINDEXFOLLOW environment variable then this must appear at the start of the file.



来源:https://stackoverflow.com/questions/43828216/x-robots-tag-noindex-specific-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!