apache2 defaultsite redirect but not virtual host
- by MMM
I'm trying to set up a new server with several virtual hosts but also such that if the requested fqdn doesn't match a virtual host then the request is redirected to http://example.com/log.php?url=fqdn
I have got the default host redirecting as desired however the virtual host that I have defined doesn't work.
I'm testing using a different host and curl -I http://hostname.example.com:8080/ on the command line to read the html headers to check for the redirect header directly rather than following it with a browser (to avoid any caching issues).
I have defined a virtualhost as the fqdn of the server but when I use curl to request that virtualhost I get redirected. If I request the server by any other name which doesn't have a virtualhost defined I also get redirected.
apache version is 2.2.16 on ubuntu
The config (concatenated together in order from a couple of different files) is as follows:
Listen 8080
NameVirtualHost *
<VirtualHost _default_>
ServerAdmin [email protected]
RewriteEngine On
RewriteRule ^(.*)$ http://example.com/log.php?url=%{HTTP_HOST}$1 [R=302,L]
</VirtualHost>
<VirtualHost *>
<Directory "/var/www">
allow from all
Options Indexes
</Directory>
DocumentRoot /var/www
ServerName hostname.example.com
</VirtualHost>
I've also tried ServerName values of hostname.example.com:* and hostname.example.com:8080
In case I wasn't clear enough:
anything.anything.any/something requested from my server should redirect to example.com/log.php?url=anything.anything.any/something
foo.example.com (not defined as a VirtualHost) requested from my server should redirect to example.com/log.php?url=foo.example.com
hostname.example.com (defined as a VirtualHost) requested from my server should return an html document
anothername.example.com (also defined as a VirtualHost) requested from my server should return an html document
It turns out that because the servers own fqdn is hostname.example.com that gets redirected to the Default VirtualHost even if there is a named VirtualHost for it. Other fqdn's that are not the same as the servers fqdn work as I intended.