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.
"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:However, it would be preferable to use mod_setenvif instead of mod_rewrite to set the environment variable:
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. (TheSetEnvIf
andHeader
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
, theNOINDEXFOLLOW
variable is not being set in the request you are seeing. After the rewrite toindex.php
Apache changes this toREDIRECT_NOINDEXFOLLOW
(REDIRECT_
prefix) and this is what you need to check for in theHeader
directive. So, in summary:(Not quite so intuitive.)
And if you use the
RewriteRule
directive instead to set theNOINDEXFOLLOW
environment variable then this must appear at the start of the file.