0

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 webmaster@zercat.net
        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.

m3z
  • 161
  • 1
  • 12
  • I think you forget to tell us what happens when you actually try to visit the site with your current setup. Or to say it differently you have told us about the state of your system, but you haven't actually told us what the symptoms of your problem are. Please give us more then just `virtual host that I have defined doesn't work.` that statement tells us nothing interesting. Do you get a DNS error, do you get a 404, 401, or something else? – Zoredache Oct 05 '12 at 00:00
  • Whoops, that's what happens when I write a question when tired. Forget something important. I'll add it now. – m3z Oct 05 '12 at 08:00
  • Oh yay, I got a downvote, would someone tell me what I did wrong so that I don't make the same mistake again? – m3z Oct 05 '12 at 08:42

1 Answers1

1

The solution to achieve what I originally wanted which is that the fqdn of the server should show a page but any fqdn for which there was not a name VirtualHost defined would be redirected as above is quite simple once I realised what the problem was.

Because the fqdn of the server was always going to show the default VirtualHost regardless whether any named VirtualHost was defined for the fqdn it meant that the solution had to be written into the default VirtualHost.

Therefore I added a rewrite condition that applies the original rewrite rule if the hostname does not match my servers fqdn. Additionally because I am using non standard ports it was necessary to define the ports in the condition as %{HTTP_HOST} includes the port number. I did this by optionally allowing any port number after the fqdn because I may change the port of the server later.

<VirtualHost _default_>
        ServerAdmin webmaster@zercat.net
        RewriteEngine On
        RewriteCond %{HTTP_HOST} !(^hostname\.example\.com[:0-9]*$) [NC]
        RewriteRule ^(.*)$ http://example.com/log.php?url=%{HTTP_HOST}$1 [R=302,L]
        <Directory "/var/www">
                allow from all
                Options Indexes
        </Directory>
        DocumentRoot /var/www
</VirtualHost>

This now works alongside other named VirtualHosts which work also.

m3z
  • 161
  • 1
  • 12