Subdomains and local development using proxy.pac

This method is out-of-date. For a far better way please read Rails subdomains, CNAMEs, and crzy.me.

The NeolithicCMS we are writing separates editions by using subdomains. It works great in production. A single entry of *.neotrib.com in the DNS points all subdomains to the same server. But for local development we have to manually edit the /etc/hosts file for each subdomain. After adding a few editions it becomes a royal pain. Short of installing a DNS server, we needed a better solution.

It turns out that a simple proxy file will do the trick. Create this file:

// # ~/.proxy.pac
function FindProxyForURL(url, host) {
  if (shExpMatch(host, "*.local")) {
    return "PROXY localhost:3000";
  }
  if (shExpMatch(host, "local")) {
    return "PROXY localhost:3000";
  }
  return "DIRECT";
}

Then open Firefox preferences under Advanced>Network>Connection>Settings…. On the Mac, add this to the Automatic proxy configuration URL:

file:///Users/USERNAME/.proxy.pac

Then in the Network settings for the Mac in the System Preferences. Click Advanced>Proxies and turn on Automatic Proxy Configuration then add to the URL:

file://localhost/Users/USERNAME/.proxy.pac

Be sure to restart Firefox and Safari before trying the proxy or it will fail. Also the local development URL is now http://local instead of http://localhost:3000. It’s actually a lot more convenient. Any subdomain like http://smithfield.local will work, too.

I wish I could take credit for this one. This is where I found it:

One BIG caveat. If you use Flash on your subdomains it can cause you problems. Flash won’t read the proxy file in Firefox. It just makes the connection directly (which won’t exist). This will make Flash utilities like SWFUpload fail if they depend on the subdomain.