I have a vhost with multiple domainnames linked to it. I'm trying to send additional headers when a request is made to one particular domainname.
Here's what I'm doing (tried both in the vhost as in .htaccess, results are the same)
SetEnvIf HOST thishost THETAG=noindex
Header set X-Robots-Tag "%{THETAG}e" env=THETAG
Header set X-Robots-Test "Yes"
So basically, the X-Robots-Test header should always be present. The X-Robots-Tag should only be present if the THETAG header is set, which is only set if the host header contains "thishost".
The RewriteRule looks something like this, so basically a 301 redirect from page1 to page2
RewriteRule page1 page2 [L,R=301]
When a request is made to page2, the headers are show correctly, but when the request is made to page1 (and redirected by mod_rewrite) the headers are not present (not even the test header).
When a redirect is initiated from a PHP script rather than mod_rewrite, it does show the headers so I assume the problem is that mod_rewrite is taking precedence on mod_headers. Is there any way to fix this?
Best Answer
As @Zimmi said in the comments, Headers are only applied on "Success" responses (HTTP 2xx codes). Redirects are 3xx codes and therefore won't get the header.
If you're using
Header set …
then useHeader always set …
. If you're using a different action (add, append, etc) then use the appropriate action.Note, though, that you shouldn't blindly "always set". From the documents:
For HSTS headers (which is what I was trying to add to domain cannonicalisation) then it seems to be the correct approach.