mod_rewrite server_name/http_host

anything

Well-Known Member
#1
Came across a strange bug today when trying to use %{SERVER_NAME} or %{HTTP_HOST} in a .htaccess RewriteCond (i tried both)

The setup was as follows:
<VirtualHost>
ServerName www.website
ServerAlias www2.website
DocumentRoot ...
</Virtualhost>

.htaccess:
RewriteEngine On
RewriteCond %{SERVER_NAME} !^www\.website$
RewriteRule robots.txt robots-goaway.txt [L]

The results:
wget -O - -q http://www2.website/robots.txt
#gets robots-goaway.txt (as expected)

wget -O - -q http://www2.website/robots.txt
#gets robots-goaway.txt (as expected)

wget -O - -q http://www.website/robots.txt
#gets robots.txt (as expected)

wget -O - -q http://www2.website/robots.txt
#gets robots.txt (incorrect!)

wget -O - -q http://www2.website/robots.txt
#gets robots.txt (incorrect!)

... now always incorrect


At first I thought our varnish config mustn't be including the domain name in the hash. But confirmed that this isnt the case. But just in case litespeed was doing something similar withsome sort of internal caching with a bad hash on url only I changed my config to be this:

<VirtualHost>
ServerName www.website
DocumentRoot ...
</Virtualhost>
<VirtualHost>
ServerName www2.website
DocumentRoot ...
</Virtualhost>

and now it works perfectly fine.

perhaps you're using the servername instead of the http host sent by the browser. please take a look.
 

mistwang

LiteSpeed Staff
#2
%{SERVER_NAME} is an internal variable using vhost ServerName.
In your case, if you expect "Host" header, you should use "%{HTTP_HOST}"
 
Top