I Love Apache

So after I got my new server online, I wanted to deal with my oversight on DSN TTLs.  Not surprisingly, mod_rewrite saved the day again.  First, since my old server is subject to the same TTLs, I added a record in my /etc/hosts file to point all of them at the new IP.  Then I changed my Apache config to run this rule set for all hosts:

RewriteEngine   On
RewriteCond     %{HTTP_HOST}   (.+)
RewriteRule     ^/(.*)$        http://%1/$1    [P,L]

Done.  That simply proxies any request with a host header through to the same exact URL, except that this time the DNS lookup is done by the old server, which has the /etc/hosts file to help it find the right place, instead of the browser that has incorrect DNS cache.  Works like a champ.

2 responses to “I Love Apache”

  1. Jamie Krug

    Barney, this is brilliant, and yet so simple! The DNS has always been the one thing that I felt I had virtually no control over when cutting over a server. This nice little trick gives you the power to make an *instant* controlled cut-over. I can't wait to cut a site over to a new server now ;-)

  2. Jamie Krug

    Thanks once again, as I just found my way back to this post :) I used this trick in an inverse fashion… A friend wanted to move their WordPress sites away from GoDaddy shared hosting to a VPS. I have the virtual host on the new VPS server proxy to the old/current shared hosting and pointed the DNS to the new box. Once DNS has had a fair amount of time to propagate, I can cut-over by setting the doc root instead of proxy to old server. This is necessary because I don't have permission to proxy from GoDaddy shared hosting (at least I assume, but could have tried it from .htaccess, but doubt they'd allow open proxies by shared hosting customers). Cheers.