Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

X Robots Tag noindex specific page

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.

like image 647
bondimedical Avatar asked Mar 09 '23 03:03

bondimedical


1 Answers

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.

like image 123
MrWhite Avatar answered Mar 30 '23 08:03

MrWhite