.htaccessmod-rewritequery-stringnoindexmod-headers

Noindex Session ID pages with htaccess


I have many pages with the parameter SID= indexed in Google which I would like to noindex, eg https://www.example.com.au/checkout/cart/?SID=c79e6055436bf371a02f4d2601cecd03

I have used the following code in htaccess but it doesn't seem to match?

RewriteCond %{QUERY_STRING} ^SID=$
RewriteRule ^(.*)$ - [env=NOINDEXFOLLOW:true]
Header set X-Robots-Tag "noindex, follow" env=NOINDEXFOLLOW

Solution

  • In your RewriteCond directive, you are trying to match against:

    ^SID=$
    

    which means, that the %{QUERY_STRING} will start with SID= and has nothing else after that. This is not the case here. Update the code to:

    RewriteCond %{QUERY_STRING} ^SID=
    RewriteRule ^ - [env=NOINDEXFOLLOW:true]
    
    Header set X-Robots-Tag "noindex, follow" env=NOINDEXFOLLOW