How to set the X-Robots-Tag HTTP header via .htaccess file based on URL query string

John Erck picture John Erck · Jan 10, 2012 · Viewed 9.6k times · Source

Is it possible to apply HTTP header directives based on the URL's query string using an apache .htaccess?

For example, based on this resource http://code.google.com/web/controlcrawlindex/docs/robots_meta_tag.html under the section titled "Practical implementation of X-Robots-Tag with Apache" it says the following .htaccess file directive can be used:

<Files ~ "\.pdf$">
  Header set X-Robots-Tag "noindex, nofollow"
</Files>

I'm looking for something along the lines of:

<QueryString ~ "m=_!">
  Header set X-Robots-Tag "noindex, nofollow"
</QueryString>

This way the following URL would NOT get indexed by search engines:

http://domain.com/?m=_!ajax_html_snippet

Any hints/tips/clues would be much appreciated. Thanks.

Answer

Ulrich Palha picture Ulrich Palha · Jan 10, 2012

You can try the following in your .htaccess file

#modify query string condition here to suit your needs
RewriteCond %{QUERY_STRING} (^|&)m=_\! [NC]
#set env var MY_SET-HEADER to 1
RewriteRule .* - [E=MY_SET_HEADER:1]

#if MY_SET_HEADER is present then set header 
Header set X-Robots-Tag "noindex, nofollow" env=MY_SET_HEADER