views:

13676

answers:

9

I want to setup my local development machine so that any requests for *.local are redirected to localhost. The idea is that as I develop multiple sites, I can just add vhosts to apache called site1.local, site2.local etc, and have them all resolve to localhost, while apache serves a different site accordingly.

I am on Windows XP.

I tried adding 127.0.0.1 *.local to my c:\windows\system32\drivers\etc\hosts file, also tried: 127.0.0.1 .local

Neither of which seem to work.

I know I can set them up on different port numbers, but that is a pain since it is hard to remember which port is which.

I don't want to have to setup a local DNS server or anything hard, any suggestions?

+6  A: 

I don't think that it is possible.

You anyway have to modify the apache virtualroot entries every time you add a new site and location, so it's not a big work to syncronise the new name to the Windows vhost file.

Biri
A: 

You can also use different directories on the same virtual host:

http://localhost/site1

http://localhost/site2

http://localhost/site3

http://localhost/site4

and so on... Using <Directory ...> directives to configure each in a different way if necessary

Vinko Vrsalovic
The problem with that is that sites often have root relative links for images, javascript, css etc... building for subdirectories makes life much more difficult.
EvilPuppetMaster
This looks like a design error for me. A site should work both from the root directory and subdirectories.
Yorirou
+1  A: 

You could talk your network administrator into setting up a domain for you (say 'evilpuppetmaster.hell') and having the wildcard there so that everything (*.evilpuppetmaster.hell') resolves to your IP

Stu Thompson
Thanks but this is on a home devserver, there is no DNS or network administrator available.
EvilPuppetMaster
OK, maybe you have access to a domain or know somebody who does? E.g.: *.evilpuppetmatser.arealdomain.com
Stu Thompson
Not really. Even so, this is a home machine so it gets a new IP everytime my router connects to the internet. An external DNS server wouldn't help.
EvilPuppetMaster
1) There is no reason *.evilpuppetmatser.arealdomain.com cannot resolve to 127.0.0.1, 2) i'm not suggesting an external DNS server. I am suggesting that you use a sub-domain on a real domain. Technically, anybody can resolve server.evp.arealdomain.com.
Stu Thompson
OK, I get what you are saying, but it doesn't help unfortunately, since I don't have access to any real domains. I need a solution that will work even with no connection to the internet.
EvilPuppetMaster
+1  A: 

I found a posting about Using the Windows Hosts File that also says "No wildcards are allowed."

In the past, I have just added the additional entries to the hosts file, because (as previously said), it's not that much extra work when you already are editing the apache config file.

Kevin Hakanson
+17  A: 

To answer your question, you cannot use wildcards in the hosts file under Windows.

However, if you want to only change the hosts file to make new sites work.... you can configure your Apache like this and you don't have to keep editing it's config:

http://postpostmodern.com/instructional/a-smarter-mamp/

Basically a quick summary based on my setup, add the following to your apache.conf file:

 LoadModule vhost_alias_module modules/mod_vhost_alias.so

 NameVirtualHost *:80

  <Directory "/xampp/sites">
      Options Indexes FollowSymLinks Includes ExecCGI
      AllowOverride All
      Order allow,deny
      Allow from all 
  </Directory>

  <VirtualHost *:80>
      VirtualDocumentRoot c:/xampp/sites/%-1/%-2+/
  </VirtualHost>

This allows me to add an entry like:

127.0.0.1       test.dev

and then make the directory, c:\xampp\sites\dev\test and place the necessary files in there and it just works.

The other option is to use <Directory> tags in apache.conf and reference the pages from http://localhost/project/.

jeremyasnyder
A: 

I could not find a prohibition in writing, but by convention, the Windows hosts file closely follows the UNIX hosts file, and you cannot put wildcard hostname references into that file.

If you read the man page, it says:

DESCRIPTION
     The hosts file contains information regarding the known hosts on the net-
     work.  For each host a single line should be present with the following
     information:

           Internet address
           Official host name
           Aliases

Although it does say,

     Host names may contain any printable character other than a field delim-
     iter, newline, or comment character.

that is not true from a practical level.

Basically, the code that looks at the /etc/hosts file does not support a wildcard entry.

The workaround is to create all the entries in advance, maybe use a script to put a couple hundred entries at once.

benc
A: 

You can use a dynamic DNS client such as http://www.no-ip.com. Then, with an external DNS server CNAME *.mydomain.com to mydomain.no-ip.com.

A: 

Have apache listen on many ports is also an alternative.

+2  A: 

Editing the hosts file is less of a pain when you run "ipconfig /flushdns" from the windows command prompt, instead of restarting your computer.

Joe