1

I want to block a query string url's in .htaccess using x-robots tag. The url's are something like:

https://www.example.com/test?limit=60
https://www.example.com/test?limit=45
https://www.example.com/test?limit=all

I need to block the ?limit=xxx

Ive tried something like this but it doesnt work:

<IfModule mod_headers.c>
<Files "^limit=?$">
Header set X-Robots-Tag "noindex, nofollow"
</Files>
</IfModule>
Blackbam
  • 17,496
  • 26
  • 97
  • 150
chappers
  • 466
  • 1
  • 6
  • 17

2 Answers2

0

Regular expressions don't work like that. Some pertinent differences:

  • ? and * are quantifiers, not catch-all "match anything" tokens.
  • ^ matches the beginning of the string.

This should work instead:

<IfModule mod_headers.c>
<Files "\?(.*&)?limit=(\d+|all)(&.*)$">
Header set X-Robots-Tag "noindex, nofollow"
</Files>
</IfModule>
Nissa
  • 4,636
  • 8
  • 29
  • 37
  • How would i write it to block multiple query strings eg: https://www.example.com/?c2c_panels=4959 https://www.example.com/?c2c_finish=4708 ? – chappers Jun 28 '18 at 10:36
  • @chappers You can probably duplicate the `` tag for that. – Nissa Jun 28 '18 at 15:20
0

Got it working with this:

<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} ^limit=([a-zA-Z0-9]*)$
RewriteRule .* - [E=NOINDEX_HEADER:1] 
</IfModule>

<IfModule mod_headers.c>
Header set X-Robots-Tag "noindex" env=NOINDEX_HEADER
</IfModule>
chappers
  • 466
  • 1
  • 6
  • 17