Question

I am starting to develop a site which basically acts as WordPress MU, in the sense that a user can signup and have their own blog. I will be coding this in Rails, however I am hoping to be able to utilize wildcard subdomains, so I can use the format such as blog.example.com. I've done some searching but I can't find any good resources.

Since I am using Rails, I'm not sure where to put this, as I am using Mongrel, and not Apache. I can bypass doing this on my local machine by developing remotely on my server, however I would only like to keep this as a last resort.

I can give more details about my development environment if needed, but here are the basics:

  • Mac OS X Leopard 10.5.6
  • Ruby 1.8.7
  • Rails 2.3.2
Was it helpful?

Solution

Strictly speaking, it's not possible to do that in the hosts file (at least on OS X). It's possible to simulate the behavior with Firefox by configuring it to use a proxy autoconfigure script.

Create a file with the following javascript (I use ~/.proxy.pac)

function FindProxyForURL(url, host) {
  if (shExpMatch(host,"*.<YOUR_DOMAIN>")) {
    //alert("proxy local")
    return "PROXY localhost";
  }
//alert("proxy direct")
return "DIRECT";
}

Then in Firefox > Preferences > Advanced > Network > Settings... > Automatic Proxy Configuration URL:

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

Never tried it in Safari, but it supports PAC files also, so perhaps it works...

The only other alternative I know is to set up a full blown DNS server on your PC...

OTHER TIPS

I couldn't get Nick's code to work with the standard localhost:3000 setup running ruby on rails on a Max OSX 10.5.8. So, I changed the function to the following. This now allows me to go to http://localhost/ and http://foo.localhost/ (and also ignores the port)

function FindProxyForURL(url, host) {
  if (shExpMatch(host, "*localhost")) {
    return "PROXY localhost:3000";
  }
  return "DIRECT";
}

Interesting - www.localhost was not working so well - firefox wanted to redirect to www.localhost.com. Something to be aware of.

I had this same problem, and it turns out it's pretty easy to get named running on OSX (it's already preinstalled!) Check out http://mikeferrier.ca/2011/04/04/setting-up-wildcard-dns-on-localhost-domains-on-osx/ for instructions.

That works beautifully, the best solution testing subdomains on localhost yet.. compare to how i used ruby ghost gem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top