I use a nifty little script to automatically generate virtual hosts on my Mac. I use them for two reasons. First, they give me nice, clean, short URLs. When I’m working on a project on my local machine, I can type in sl as the address of my site instead of localhost/sandlot. The second reason why is so I can use root-relative paths on my local server just as I will on my remote server. So to reference my images directory, I can just use /ui/images instead of trying to keep track of where I am with respect to the document root (e.g. ../../../ui/images/).
This works perfectly in Apache on my Mac, but I recently installed Virtualbox so I could test stuff in Internet Explorer. Referencing your local server from within Virtualbox hasn’t been exactly clear, and I found I had to use my Mac’s IP address and reference the Sites folder within the Home folder. The primary problem with this is that all the root-relative paths break because the URL in Windows doesn’t follow the same pattern. sl is much different than 10.0.1.2/~Clifton/Sites/sandlot.
After doing some research, I discovered a way to make Windows see my pretty little virtual hosts. Here’s how.
Access your hosts file here:
C:\windows\system32\drivers\etc
Edit that file with Notepad, and at the bottom, delete the line that has 127.0.0.1 on it, and add a new line for each virtual host you have like this:
10.0.2.2 name
The 10.0.2.2 is a default IP address that Virtualbox uses to reference your Mac (assuming you’re using the default NAT networking mode like I am), and the “name” is the name of your virtual host. So my full list looks something like this:
10.0.2.2 sl
10.0.2.2 clifton
10.0.2.2 cubs
10.0.2.2 labrum
10.0.2.2 labrums
10.0.2.2 lance
Now I can view local files instead of pushing things to a remote server in order for the paths to work. It also saves keystrokes as I can use the short names in Windows just as I do on my Mac.
Nice find. Noted.
Woot! Thanks very much for this. Didn’t realise that VirtualBox assigned a new IP.