Skip to main content

Wildcards in a hosts file


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?


Source: Tips4allCCNA FINAL EXAM

Comments

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

    ReplyDelete
  2. 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/.

    ReplyDelete
  3. 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.

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

    ReplyDelete
  5. Acrylic DNS Proxy (free, open source) does the job. It creates a proxy DNS server (on your own computer) with its own hosts file. The hosts file accepts wildcards.

    http://mayakron.altervista.org/support/browse.php?path=Acrylic&name=Home

    ReplyDelete
  6. 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

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

    ReplyDelete
  8. Have apache listen on many ports is also an alternative.

    ReplyDelete
  9. I have written a simple dns proxy in Python. It will read wildcard entries in /etc/hosts. See here: http://code.google.com/p/marlon-tools/source/browse/tools/dnsproxy/dnsproxy.py

    I have tested in Linux & Mac OS X, but not yet in Windows.

    ReplyDelete
  10. You may try AngryHosts, which provided a way to support wildcard and regular expression. Actually, it's a hosts file enhancement and management software.
    More features can be seen @ http://angryhosts.com/features/

    ReplyDelete
  11. Following on from @Stu Thompsons answer, please feel free to use my domain anySubDomain.reconn.co.uk. This will point any sub domain to your local host 127.0.0.1. I will leave this up and running if it is of any use.

    A workaround that works if you're online...

    ReplyDelete
  12. 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

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.