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).


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值