How to alias a hostname on Mac OSX

70,569

Just to be clear, I'm basing this on the assumption that you really do want http://local.example.com to load the literal web page http://localhost/path/to/example.com. In other words, this will only work for this machine. If, on the other hand, you're trying to serve web pages to the outside world using your Mac OS X machine, then that's a different question.

First, add a new line to your /etc/hosts file:

127.0.0.1   local.example.com

You can do this by running the command sudo nano /etc/hosts, add this line to the end, then save it by pressing Ctrl-X, Y.

How you actually redirect/alias the address http://local.example.com to http://localhost/path/to/example.com/ depends on which web server you're using. Assuming you're using Apache:

If you want the user's browser to show local.example.com, then you want to set up a virual host and your httpd.conf file should have something like the following:

<VirtualHost *:80>
ServerName local.example.com
DocumentRoot /www/path/to/example.com
</VirtualHost>

If, on the other hand, you want the web browser's location bar to change to http://localhost/path/to/example.com/, then instead you will want to use mod_rewrite to create a redirect:

RewriteCond %{HTTP_HOST}   !^local\.example\.com [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteRule ^/?(.*)         http://localhost/path/to/example.com/$1 [L,R,NE]
Share:
70,569

Related videos on Youtube

Austin Hyde
Author by

Austin Hyde

Updated on September 17, 2022

Comments

  • Austin Hyde
    Austin Hyde almost 2 years

    In a nutshell, I would like to be able to open a browser and open local.example.com but it actually loads http://localhost/path/to/example.com/

    I am using Mac OSX 10.5, and not afraid to get my hands dirty with the terminal :)

    I use Apache as my local server.

    • Stephen Jennings
      Stephen Jennings about 14 years
      Are you trying to serve web pages to the outside world, or are you only expecting the local.example.com URL to work for your machine?
    • Austin Hyde
      Austin Hyde about 14 years
      Just for my own machine. It gets tiresome typing/remembering the full filepath to my local copy of whatever websites I'm working on.
  • John T
    John T about 14 years
    This won't go to a certain path though
  • Stephen Jennings
    Stephen Jennings about 14 years
    @John you're right, fixed.
  • Kevin Meredith
    Kevin Meredith over 6 years
    If the desired "alias" were https://localhost/path/to/example.com, i.e. HTTPS, not HTTP, would this solution still apply?
  • Beetle
    Beetle almost 6 years
    @KevinMeredith that's not possible. The browser has to know that it's talking HTTPS. It might be possible with a HTTP 302 redirect, which in Apache is called Redirect. httpd.apache.org/docs/2.4/mod/mod_alias.html#redirect
  • Coder Guy
    Coder Guy over 4 years
    /etc/hosts usually gets overwritten if you're using a VPN though