Apache mod_substitute for Dev Server Installs

I had to set up a development site for a client today were the URLs were hardcoded in the templates and CSS files. The client wasn’t willing to pay for making the base URL a config option. Adding the domain to the hosts file wasn’t an option because several people coming from different IPs needed to be able to use the dev site. To avoid people getting bounced back to the live site when clicking links and preventing them from loading resources from the live site via the hardcoded paths in the CSS files another solution was necessary. After looking around for a while I found mod_substitute which does exactly what I want. Before sending the output to the browser Apache replaces strings in the output via regular expressions. For performance reasons you don’t want to run this on your live site but it shouldn’t be noticeable during development.

To enable the substitution for HTML/CSS and JS files simply install mod_substitute and add this to your vhost:

<Location />
    AddOutputFilterByType SUBSTITUTE text/html
    AddOutputFilterByType SUBSTITUTE text/css
    AddOutputFilterByType SUBSTITUTE text/js
    Substitute s/www\.foo\.com/dev.foo.com/n
Apache mod_substitute for Dev Server Installs