323

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?

Simon East
  • 55,742
  • 17
  • 139
  • 133
EvilPuppetMaster
  • 8,072
  • 10
  • 34
  • 31
  • 1
    Please feel free to use anysubdomain.reconn.co.uk as a work around (if you're online), which will always point to your localhost (see my answer below). – Paul Grimshaw Mar 16 '12 at 23:56
  • what about a script that parses the apache vhosts ServerName and updates the hosts file ? – E Ciotti Aug 06 '13 at 12:45
  • 1
    I believe *.local has special semantics, and you should use something non-existent. I use *.dev since I don't think that's real. – WraithKenny Sep 19 '13 at 17:52

20 Answers20

463

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.

Download from the offical website

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

Configuring Acrylic DNS Proxy

To configure Acrylic DNS Proxy, install it from the above link then go to:

  1. Start
  2. Programs
  3. Acrylic DNS Proxy
  4. Config
  5. Edit Custom Hosts File (AcrylicHosts.txt)

Add the folowing lines on the end of the file:

127.0.0.1   *.localhost
127.0.0.1   *.local
127.0.0.1   *.lc

Restart the Acrylic DNS Proxy service:

  1. Start
  2. Programs
  3. Acrilic DNS Proxy
  4. Config
  5. Restart Acrylic Service

You will also need to adjust your DNS setting in you network interface settings:

  1. Start
  2. Control Panel
  3. Network and Internet
  4. Network Connections
  5. Local Area Connection Properties
  6. TCP/IPv4

Set "Use the following DNS server address":

Preferred DNS Server: 127.0.0.1

If you then combine this answer with jeremyasnyder's answer (using VirtualDocumentRoot) you can then automatically setup domains/virtual hosts by simply creating a directory.

PrimosK
  • 13,848
  • 10
  • 60
  • 78
Petah
  • 45,477
  • 28
  • 157
  • 213
  • 15
    Just to be clear, the "custom hosts file" is AcrylicHosts.txt, not AcrylicConfiguration.ini. Maybe that should be obvious, but it confused me for a while. – mhenry1384 May 07 '13 at 14:07
  • @Petah - this exact configuration was working for me with wildcard domains, but it just stopped and I can't figure out why. All requests return "This page is unavailable". Restarted Acryllic, reset all caches... what else do you think it could be? – SB2055 Jan 04 '14 at 00:02
  • 1
    @SB2055 check your DNS settings in your network properties is still correct. – Petah Jan 05 '14 at 00:30
  • It should be noted that Acrylic is not well suited to situations where your network environment changes frequently. Google's public DNS server won't resolve real machine names within your network... and you can fix that for one network, but you still won't be able to resolve internal machine names when you go to another network. This can be especially painful. But I should also point out that I don't know everything there is to know about Acrylic. – Alex Dresko Apr 28 '15 at 13:41
  • If this is done within a VM, then the `PrimaryServerAddress` in the configuration (not the hosts file) should be changed from the default `8.8.8.8` to the one the host provides. In VirtualBox, this is `10.0.2.3`. – Shahar 'Dawn' Or Jul 31 '15 at 22:58
  • @MoxShah You need to use a wildcard like `*.mydomain.working` – Petah Feb 26 '17 at 06:39
  • 1
    I think Acrylic no longer works on newer versions of Windows 10 due to the Internet Connection Sharing (ICS) service. (If you can make it work please let me know) – Taylan Jan 12 '20 at 23:14
  • @Taylan Simply stop the `Host Network Service` then you can `Stop` and `Disable` ICS and then re-enable `Host Network Service`. Now restart Acrylic and you're done. – Ohgodwhy Sep 11 '20 at 18:47
  • @Ohgodwhy how to stop `Host Network Service` ? – Svetoslav Stefanov Apr 06 '21 at 07:51
  • It might be in a connection that is not `Local Area Connection Properties.` – Pedro Rodrigues Jun 22 '22 at 17:28
  • you can also get Acrylic DNS through `winget install Mayakron.AcrylicDNS` – Steve Oct 06 '22 at 19:59
84

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
  • 1,379
  • 1
  • 8
  • 8
33

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.

Update: please check the next answer and the comments on this answer. This answer is several years old and not correct anymore.

Christos Lytras
  • 36,310
  • 4
  • 80
  • 113
Biri
  • 7,101
  • 7
  • 38
  • 52
  • 3
    But when using Nginx it's not necessary to change configure file of Nginx to add a new group of second level domains *.localhost. So, it's the minus of hosts file. – sergzach Feb 19 '12 at 06:58
  • 9
    Just to point out that when you have a multilingual website, you might have a with `ServerAlias *.mydomain.localhost` (`*` being the language code), so that you don' have to edit httpd.conf each time you add a new language; but you still need to add the subdomain to the hosts file, hence the relevance of the question above. – BenMorel Mar 27 '12 at 09:05
  • 10
    Dynamic Virtual Hosts are the keyword here and are supported by most servers, i.e. Apache and Nginx. So no, you definitely don't have to touch your Apache-config every time you create a project. – Valentin Klinghammer Dec 11 '12 at 11:19
  • 2
    We can have wildcard virtual hosts too, it's only down to the `hosts` file really – ericn Dec 28 '12 at 06:59
7

To add to the great suggestions already here, XIP.IO is a fantastic wildcard DNS server that's publicly available.

      myproject.127.0.0.1.xip.io  -- resolves to -->   127.0.0.1
  other.project.127.0.0.1.xip.io  -- resolves to -->   127.0.0.1
   other.machine.10.0.0.1.xip.io  -- resolves to -->   10.0.0.1

(The ability to specify non-loopback addresses is fantastic for testing sites on iOS devices where you cannot access a hosts file.)

If you combine this with some of the Apache configuration mentioned in other answers, you can potentially add VirtualHosts with zero setup.

Simon East
  • 55,742
  • 17
  • 139
  • 133
6

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
  • 69
  • 1
  • 1
  • 1
    it does not flush browser dns cache, but window's. So he has to wait 20-30 mins for the browser to release dns caching. – Jacob Melvad Jensen Nov 29 '12 at 11:59
  • When set to use system proxy settings, Firefox is completely transparent after a "ipconfig /flushdns" command (on Windows 10 in 2020). – Spyryto Feb 24 '21 at 11:48
5

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
  • 41,386
  • 23
  • 126
  • 155
  • The longer you make the hosts file, the longer it will take to resolve requests. Not a good solution if you've got a lot of entries. – cchamberlain Jun 08 '16 at 20:51
4

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
  • 38,370
  • 19
  • 110
  • 156
  • 1
    Thanks but this is on a home devserver, there is no DNS or network administrator available. – EvilPuppetMaster Sep 26 '08 at 08:04
  • OK, maybe you have access to a domain or know somebody who does? E.g.: *.evilpuppetmatser.arealdomain.com – Stu Thompson Sep 26 '08 at 08:10
  • 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 Sep 26 '08 at 08:13
  • 2
    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 Sep 26 '08 at 08:19
  • 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 Sep 30 '08 at 08:04
  • 3
    There are also some public ones like *.127.0.0.1.xip.io and [others](http://stackoverflow.com/q/1562954/167815) – Dave James Miller Feb 27 '13 at 23:03
4

We have this working using wildcard DNS in our local DNS server: add an A record something like *.local -> 127.0.0.1

I think that your network settings will need to have the chosen domain suffix in the domain suffix search list for machines on the network, so you might want to replace .local with your company's internal domain (e.g. .int) and then add a subdomain like .localhost.int to make it clear what it's for.

So *.localhost.int would resolve to 127.0.0.1 for everybody on the network, and config file settings for all developers would "just work" if endpoints hang off that subdomain e.g. site1.localhost.int, site2.localhost.int This is pretty much the scheme we have introduced.

dnsmasq also looks nice, but I have not tried it yet: http://ihaveabackup.net/2012/06/28/using-wildcards-in-the-hosts-file/

Matthew Skelton
  • 2,220
  • 21
  • 21
3

I'm using DNSChef to do that.

https://thesprawl.org/projects/dnschef/

You have to download the app, in Linux or Mac you need python to run it. Windows have their own exe.

You must create a ini file with your dns entries, for example

[A]
*.google.com=192.0.2.1
*.local=127.0.0.1
*.devServer1.com=192.0.2.3

Then you must launch the dns application with admin privileges

sudo python dnschef.py --file myfile.ini -q

or in windows

runas dnschef.exe --file myfile.ini -q

Finally you need to setup as your only DNS your local host environment (network, interface, dns or similar or in linux /etc/resolv.conf).

That's it

casivaagustin
  • 558
  • 5
  • 7
3

I made this simple tool to take the place of hosts. Regular expressions are supported. https://github.com/stackia/DNSAgent

A sample configuration:

[
    {
        "Pattern": "^.*$",
        "NameServer": "8.8.8.8"
    },
    {
        "Pattern": "^(.*\\.googlevideo\\.com)|((.*\\.)?(youtube|ytimg)\\.com)$",
        "Address": "203.66.168.119"
    },
    {
        "Pattern": "^.*\\.cn$",
        "NameServer": "114.114.114.114"
    },
    {
        "Pattern": "baidu.com$",
        "Address": "127.0.0.1"
    }
]
Stackia
  • 2,110
  • 17
  • 23
3

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.

marlonyao
  • 131
  • 2
3

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/

iloahz
  • 4,491
  • 8
  • 23
  • 31
1

@petah and Acrylic DNS Proxy is the best answer, and at the end he references the ability to do multi-site using an Apache which @jeremyasnyder describes a little further down...

... however, in our case we're testing a multi-tenant hosting system and so most domains we want to test go to the same virtualhost, while a couple others are directed elsewhere.

So in our case, you simply use regex wildcards in the ServerAlias directive, like so...

ServerAlias *.foo.local
oucil
  • 4,211
  • 2
  • 37
  • 53
1

Here is the total configuration for those trying to accomplish the goal (wildcards in dev environment ie, XAMPP -- this example assumes all sites pointing to same codebase)

hosts file (add an entry)

file: %SystemRoot%\system32\drivers\etc\hosts

127.0.0.1   example.local

httpd.conf configuration (enable vhosts)

file: \XAMPP\etc\httpd.conf

# Virtual hosts
Include etc\extra\httpd-vhosts.conf

httpd-vhosts.conf configuration

file: XAMPP\etc\extra\httpd-vhosts.conf

<VirtualHost *:80>
    ServerAdmin admin@example.local
    DocumentRoot "\path_to_XAMPP\htdocs"
    ServerName example.local
    ServerAlias *.example.local
#    SetEnv APP_ENVIRONMENT development
#    ErrorLog "logs\example.local-error_log"
#    CustomLog "logs\example.local-access_log" common
</VirtualHost>

restart apache

create pac file:

save as whatever.pac wherever you want to and then load the file in the browser's network>proxy>auto_configuration settings (reload if you alter this)

function FindProxyForURL(url, host) {
  if (shExpMatch(host, "*example.local")) {
    return "PROXY example.local";
  }
  return "DIRECT";
}
Community
  • 1
  • 1
Daniel Jordi
  • 183
  • 1
  • 2
  • Didn't work, don't know about PAC yet but this line 'return "PROXY example.local";' wouldn't return a proxy ip:port to be used by the browser as proxy ? – ion Oct 30 '17 at 09:50
1

You can use echoipdns for this (https://github.com/zapty/echoipdns).

By running echoipdns local all requests for .local subdomains are redirected to 127.0.0.1, so any domain with xyz.local etc will resolve to 127.0.0.1. You can use any other suffix also just replace local with name you want.

Echoipdns is even more powerful, when you want to use your url from other machines in network you can still use it with zero configuration.

For e.g. If your machine ip address is 192.168.1.100 you could now use a domain name xyz.192-168-1-100.local which will always resolve to 192.168.1.100. This magic is done by the echoipdns by looking at the ip address in the second part of the domain name and returning the same ip address on DNS query. You will have to run the echoipdns on the machine from which you want to access the remote system.

echoipdns also can be setup as a standalone DNS proxy, so by just point to this DNS, you can now use all the above benefits without running a special command every time, and you can even use it from mobile devices.

So essentially this simplifies the wildcard domain based DNS development for local as well as team environment.

echoipdns works on Mac, Linux and Windows.

NOTE: I am author for echoipdns.

arva
  • 2,384
  • 1
  • 17
  • 14
0

While you can't add a wildcard like that, you could add the full list of sites that you need, at least for testing, that works well enough for me, in your hosts file, you just add:

127.0.0.1 site1.local
127.0.0.1 site2.local
127.0.0.1 site3.local
...

John Saunders
  • 160,644
  • 26
  • 247
  • 397
BlackTigerX
  • 6,006
  • 7
  • 38
  • 48
0

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
  • 1,381
  • 5
  • 31
  • 39
0

Configuration for nginx config auto subdomain with Acrylic DNS Proxy

auto.conf file for your nginx sites folder

server {
    listen 80;
    server_name ~^(?<branch>.*)\.example\.com;
    root /var/www/html/$branch/public;  

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_log  /var/log/nginx/$branch.error.log error;

    sendfile off;

    client_max_body_size 100m;

    location ~ \.php$ {
       try_files $uri /index.php =404;
       fastcgi_pass php-fpm:9000;
       fastcgi_index index.php;
       fastcgi_buffers 16 16k;
       fastcgi_buffer_size 32k;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

Add to Acrylic hosts file 127.0.0.1 example.com *.example.com and restart Acrylic service. $branch - your subdomain name.

Set instead of root /var/www/html/$branch/public; your project path

Davron Achilov
  • 536
  • 4
  • 14
0

This can be done using Pi-Hole, just edit the "/etc/hosts" and restart dns service.

nano /etc/hosts
pihole restartdns

Example:

127.0.1.1       raspberrypi
192.168.1.1     w1.dev.net
192.168.1.2     w2.dev.net
192.168.1.3     w3.dev.net
-1

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.

  • 2
    that would resolve to your external ip, and usually that would just bring up your router/modems config page – Sam Jan 17 '11 at 15:09