3

I find it confusing to work on my site locally when there's something like "localhost:80" in the adress bar. It would be much better to have something like "mydomainoffline.com" which then maps somehow to "localhost:80", if possible. How would I do that on the MAC?

I use the free MAMP package (mamp.info), not the commercial MAMP PRO. So I need to do that sort of thing by hand. How can I do that?

openfrog
  • 235
  • 1
  • 4
  • 8

5 Answers5

7

I realize that the OP has a mac, and that the solutions given work for both mac and linux.

But I would also like to add that Windows has a hosts file too in C:\Windows\System32\drivers\etc\hosts, in case someone comes here through google with the same problem and uses windows :)

Andreas Bonini
  • 1,292
  • 1
  • 9
  • 16
4

The Webserver can't do that for you, as it's not responsible for DNS resolution.

You'll need to add an entry to your Hosts file (/private/etc/hosts or /etc/hosts depending on the version), and then perform some special MacOS X magic ritual to make it stick.

  • Leopard (10.5) (and presumably 10.6) has no `niload` so I'm not sure if this applies any more. –  Dec 21 '09 at 12:26
2

You can do this by editing /etc/hosts (you will need to do this as administrator, e.g. sudo nano /etc/hosts

Find the line that looks like:

127.0.0.1    localhost

And add another hostname, e.g.

127.0.0.1    localhost mydomainoffline.com

(Edit: I should point out that while this works just fine for Leopard (10.5), I don't know about other versions of Mac OS X)

1

Edit your HOSTS file (/etc/hosts) Replace localhost by the desired name

And you don't need to have :80 because it is the default HTTP port anyway

Finder->Go To-> /etc/

Then open up the hosts file with TextEdit and do what I said above

Kristina Brooks
  • 137
  • 1
  • 1
  • 7
  • Given that the OP is using a Mac OS X package, I don't think they'll find a hosts file there. – Quentin Dec 21 '09 at 12:13
  • Sorry , I didn't read the question properly , fixed – Kristina Brooks Dec 21 '09 at 12:15
  • 5
    There is absolutely no need to **replace** the `localhost` entry with something else; in fact, this is a bad idea and could break a lot of things. –  Dec 21 '09 at 12:19
  • 2
    Still not correct, the path is different on MacOS, and editing the file is not sufficient. Besides, I would advise very much against *replacing* the localhost entry, because a lot of programs depend on localhost being there. – Michael Borgwardt Dec 21 '09 at 12:19
  • 1
    I would not recommend replacing localhost - you could instead append another domain name for localhost. –  Dec 21 '09 at 12:20
  • 5
    did anyone mention that it's not a good idea to replace `localhost` ? – pavium Dec 21 '09 at 12:23
1

I don't use MAMP, but I have done the same thing using the pre-installed Apache 2. There may be a simpler way to do this, but this has really helped me with testing multiple sites in my local machine.

Edit host file

The host file is located at /etc/hosts. I end all of my local test domains with local, but this isn't required. Here's a sample of what I have.

# clients
    127.0.0.1       acmewidgets.clients.local
    127.0.0.1       someclient.clients.local
    127.0.0.1       etcetc.clients.local


# Projects and mini sites
    127.0.0.1       someproject.proj.local
    127.0.0.1       someotherproject.proj.local

# tools
    127.0.0.1   sql.tools.local

# Different open source solutions
    # e-commerce
        127.0.0.1       magento.apps.local
        127.0.0.1       opencart.apps.local
        127.0.0.1       oscommerce.apps.local
        127.0.0.1       zencart.apps.local
    # forums
        127.0.0.1       vanilla.apps.local
    # blogs
        127.0.0.1       wp.apps.local
        127.0.0.1       wpmu.apps.local
    # CMS's
        127.0.0.1       joomla.apps.local
        127.0.0.1       drupal.apps.local
        127.0.0.1       concrete5.apps.local

So not only do I have different client sites, but you can see that I have a subset of open source platforms that I routinely use and test against, as well as personal projects and tools.

Also, note that it's not a good idea to replace localhost, as some other applications are likely to use it. You should just add

Edit virtual hosts

Again, I don't use MAMP so I don't know where it's stored, but you're looking for the http-vhosts.conf file. In the standard OS X install, it's located at /etc/apache2/extra/httpd-vhosts.conf

For every *.local domain that I've created in my host file, I've created a respective virtual host entry.

NameVirtualHost *:80

# Open source app testing
# ----------------------------------------------------------------------

<VirtualHost *:80>
    ServerName opencart.apps.local
    DocumentRoot /Users/justin/Development/localhost/opencart/html
</VirtualHost>

<VirtualHost *:80>
    ServerName oscommerce.apps.local
    DocumentRoot /Users/justin/Development/localhost/oscommerce/html
</VirtualHost>

# Tools
# ----------------------------------------------------------------------

<VirtualHost *:80>
    ServerName sql.tools.local
    DocumentRoot /Users/justin/Development/localhost/bin/tools/wwwsqldesigner
</VirtualHost>

* You may have to change the permissions on either of these files to be able to save your changes.

Justin Johnson
  • 143
  • 1
  • 9