Simple Virtual Server redirects
By jmccabe on May 24, 2006
Several years ago a photograph that I took was used in a documentary. Being conscientious folk the documentary creators placed in their credits links back to my web site. Unfortunately they didn't have me proof the link before they pressed a couple thousand DVDs, and what should have been:
ended up as:
Since it was too late to fix their mistake the obvious solution seemed to be to create a DNS record for www.photos.baz.com and have the Virtual Server for photos.baz.com answer the requests for either name. This annoyed me though - I don't want a typo URL to become an authoritative URL. Surely the Right Thing To Do would be to redirect people that visit that wrong URL to the correct URL! Yes, of course!
So I began thinking about how to do this redirect. Essentially I want every URI that comes to the bad DNS to be redirected to the good DNS including "/", and I wanted the URI and query strings to be preserved. So how to accomplish this?
The obvious solution is to create a Virtual Server for the bad DNS and have its entire default object look like:
<Object name=default> Nametrans fn=redirect from=/ url=http://photos.baz.com/ </Object>
and the appropriate entries in the server.xml. But this solution just bugs me. I hate having an entire Virtual Server defined that does nothing but redirect. Surely there's a better way.
The solution I ultimately ended up using doesn't use a dedicated Virtual Server at all. Instead it abuses the Client tag and the Default Virtual Server.
Within the obj.conf that belongs to my Default Virtual Server I have this Client tag:
<Client urlhost="www.photos.baz.com"> NameTrans fn="redirect" from="/" url-prefix="http://photos.baz.com/" </Client>
So the DNS record for the bad host points to this machine, but the Web Server has no knowledge of the Virtual Server to handle "www.photos.baz.com" and uses the Default Virtual Server. The Client tag then matches the Host header and redirects the User-Agent to the proper DNS name with a HTTP 302. The User-Agent then corrects the Location bar and submits the request again using the correct URL. This time the appropriate Virtual Server is tickled via the now correct Host header. Yay!
This is a technique I use in a few places to provide very simple vanity Virtual Servers. I could just as well create a single Virtual Server, however, that contains only the vanity redirects (as described above) and owns the various vanity/typo host names in server.xml. This would allow me to remove these redirects from the obj.conf of my Default Virtual Server.
I suppose it stands to reason that Web Server 7 has some simpler ways to accomplish this. I'll write about those shortly.