57

As you surely know you can map host names to IP addresses with the "hosts" file. This is very useful especially when you are developing as you can change localhost for project1.

That part is clear, but unfortunately I can't figure out how to set this up to open multiple projects with the same IP. I've tried a few things without success such as:

127.0.0.1/projects/project1  project1
127.0.0.1/projects/project2  project2

I checked some related questions but I couldn't find an answer. So, can I accomplish what I'm trying to do somehow? Any suggestions?

Boann
  • 48,794
  • 16
  • 117
  • 146
Vinicius Santana
  • 3,936
  • 3
  • 25
  • 42

3 Answers3

87

The hosts file is only a mapping from names to an IP. You can specify multiple names next to an IP like this:

127.0.0.1 name1 name2 name3.domain
Boann
  • 48,794
  • 16
  • 117
  • 146
DRC
  • 4,898
  • 2
  • 21
  • 35
  • ok, but how do i get to specific folders? I have multiple project on my localhost. localhost/name1 localhost/name2 I guess this is the right question. How do I get name1 to point to localhost/name1? – Vinicius Santana Jul 06 '13 at 18:30
  • 5
    it is just a dns like mapping, so it does not have any notion of path or apache like subdirs. – DRC Jul 06 '13 at 19:29
  • 4
    You need setting virtual host in Apache config. – Vy Do Nov 10 '14 at 14:09
  • 2
    Worth noting that on Windows this will work for up to 9 aliases - see [my answer](https://stackoverflow.com/questions/17505835/55628047#55628047) below. – Steve Chambers Jan 08 '20 at 15:08
56

I got this resolved thanks to Google and the collaborators, @jvilhena and @DRC. Here's how I did it:

If you are using Windows and XAMPP as in my case the first step is to set up the 'hosts' file. If you are using Windows it's likely that you will find it in C:\Windows\System32\drivers\etc\hosts. You can use any text editor to edit it.

You can set up as many host names as you like all pointing to your localhost, with the IP, 127.0.0.1.

For example:

 127.0.0.1               local.project1
 127.0.0.1               local.project2
 127.0.0.1               youcanuseany.name.here

The second step was to deal with the Apache file httpd-vhosts.conf. Again, I'm using Windows and XAMPP. It's likely this file will be in C:\xampp\apache\conf\extra\httpd-vhosts.conf.

You don't have to but I like to keep my project folders in my htdocs folder @ C:\xampp\htdocs.

For each project that you create a "host name" for, you should add the following to your httpd-vhosts.conf file:

<VirtualHost *>
    DocumentRoot "C:\xampp\htdocs\projectx"
    ServerName youcanuseany.name.here
    <Directory "C:\xampp\htdocs\projectx">
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>  

One more example just for the sake of it :)

<VirtualHost *>
    DocumentRoot "C:\xampp\htdocs\project1"
    ServerName local.project1
    <Directory "C:\xampp\htdocs\project1">
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

Now you can type local.project1 and youcanuseany.name.here in your browser and it should open your project as if you were typing localhost/project1 and localhost/projectX. I hope this helps.

Boann
  • 48,794
  • 16
  • 117
  • 146
Vinicius Santana
  • 3,936
  • 3
  • 25
  • 42
  • 9
    Don't forget to edit your httpd.conf-file so that Apache actually loads information about the virtual hosts. To do that, look for and uncomment this line: `#Include /Applications/XAMPP/etc/extra/httpd-vhosts.conf` by removing the `#`-symbol. I found that piece of advice at [F6 Design](http://f6design.com/journal/2012/03/11/configuring-virtualhosts-in-xampp-on-mac/) at that brought this answer to home base! :) – erlingormar Dec 05 '13 at 00:31
  • 2
    and you must restart Apache to see result. – Vy Do Nov 10 '14 at 14:08
  • `sudo service apache2 restart` – SDsolar Jun 07 '18 at 04:26
  • Update for 2021: The syntax: "Order allow,deny / Allow from all" changed for Apache 2.4 and after and is now "Require all granted" If needs be you an use if "IfVersion" derective (load appropriate module) cover both cases in a generic .conf file. – Duke Bouvier Feb 25 '21 at 22:12
26

[Windows only]

There are two highly voted answers - one putting all the aliases on a single line and the other putting them on separate lines. It's worth noting that whilst the first solution is more compact, the second may also be needed in a Windows environment since Windows has a limit of 9 for the number of aliases on each line. So combine these two methods to have maximum compactness but still work when there are a lot of aliases:-

127.0.0.1 alias1 alias2 alias3 alias4 alias5 alias6 alias7 alias8 alias9
127.0.0.1 alias10 alias11 alias12 alias13 alias14 alias15 alias16 alias17 alias18 
127.0.0.1 alias19 ...etc...
Steve Chambers
  • 37,270
  • 24
  • 156
  • 208