VirtualHost Tricks - Pointing your machine's IP to a different location than localhost

VirtualHost Tricks - Pointing your machine's IP to a different location than localhost

When you run a web server on your personal machine, you can access your web documents a few ways: using http://localhost or http://127.0.0.1, virtual hosts (http://whatever), and the internal IP of the machine. Unless you are directly plugged into the internet, the internal IP is the address the router provides. The format is usually 192.168.0.xxx, where the xxx is unique to each computer. But what happens when you want localhost and 192.168.0.xxx to point to different locations? Apache treats 192.168.0.xxx the same as localhost, because it is the same---when you define document locations and configurations on production sites you use 127.0.0.1 (localhost) which points to the IP of the machine. The reason you don't use the direct IP address is because it can easily change. For me, setting the internal IP of my machine to point to a different location is very important. I want to be able to develop my personal sites under localhost, while developing all my work sites under a virtual host. But I also want the internal IP to point to my work sites, that way if I need to test a site on another computer or send works-in-progress to co-workers, the IP would point to my work sites and not my personal sites. Turns out the solution is pretty simple. Here are the steps I took in order to get localhost directing one place and the internal IP directing another:
  1. First, set your document root to your personal Sites directory (in the case of Mac it is /Users/Your Username/Sites
  2. Then setup a virtual host for the work Sites directory:
    <VirtualHost 127.0.0.1:80> DocumentRoot "/Users/Your Username/Work/Sites" ServerName workdev </VirtualHost>
  3. Next (and this is the trick), setup mod_rewrite to redirect all requests to your internal IP to the work Sites folder:
    # Rewrite 192.168.0.* to Work Sites directory # but only for port 80, in case other projects # are running off localhost on different ports RewriteEngine On RewriteCond %{SERVER_ADDR} ^192\.168\.0\. RewriteCond %{SERVER_PORT} ^80$ RewriteRule ^/(.*)$ /Users/Your Username/Work/Sites/$1
    You'll notice that the SERVER_ADDR is set to 192.168.0. This is so if your machine gets assigned a different xxx it will still redirect. I also set a condition for port 80 only (www). This allows for sites running under different ports (in the case of Rails, Merb, etc.) to be accessed using 192.168.0.xxx:3000.
  4. Last things last, update your hosts file (/etc/hosts on mac) to include the following line:
    127.0.0.1 workdev
    This will allow you to access your work Sites using the http://workdev virtual host.
Note: You can do this using the Listen declaration; however, this doesn't allow you to specify all options for the last number of the ip (192.168.0.xxx).

Related Posts

Tricks for a Better Memory

Rachel
Read more

11 Location Based Applications for Your iPhone

Colin
Read more

My Top 5 Favorite Drupal Development Workflow Tricks

Kyle Taylor
Read more

Tools, Tips and Tricks on Being a Great SEO, Social Media Expert, or Internet Marketing Specialist

Tonya Cauduro
Read more

Installing Drupal 7 using SSH & Drush Tutorial

Tom McCracken
Read more

5 Tips for Higher Local Business Rankings in Google Places

Erin Tuohy
Read more