I need to test sub-domains on my localhost. How can I effectively have this result of adding *.localhost.com to my /etc/hosts/ file?

If it's not possible, how do I work around this problem? I need to test wildcard sub-domains on my localserver. It is a Django devserver, can the Django dev server handle the sub-domains? Can some other piece of software/routing give me the end result I want?

  • 1
    Belongs on superuser ? – Paul R Mar 2 '10 at 16:48
  • I know people say it's not possible! But how do I do it anyway! What is something outside of /etc/hosts/ I can use to get the effect. I'm testing a development server. – MikeN Mar 2 '10 at 20:17
  • As most answers are focusing on your first question (localhost subdomain wildcards), I'll answer your secondary question as a comment: yes, the Django dev server is perfectly capable of handling localhost subdomains, you just have to convince your browser and your OS to send the traffic its way (using one of the various solutions below)! – hheimbuerger Mar 9 '18 at 21:22

15 Answers 15


I have written a dns proxy in Python. It will read wildcard entries in /etc/hosts. See here: https://github.com/hubdotcom/marlon-tools/blob/master/tools/dnsproxy/dnsproxy.py

  • This is perfect! I have been looking for a simple solution like this for a long time (Working on OSX Mavericks BTW) – Billy Moon Nov 5 '13 at 22:07
  • 2
    now, if we could only just pip install it :) – metakermit Mar 25 '14 at 11:39
  • 1
    I installed this, did everything and ran it, but it does not block sites. – DisplayName Dec 13 '15 at 18:47
  • Can not upvote this enough. I've needed this forever, stumbled upon this gem of a question and answer, and I'm a bit of a Python hacker, and this is a neat tidbit. Thanks for sharing! – Farley Sep 8 '16 at 15:41

Install dnsmasq (I do this on all my Linux desktops as a DNS cache anyways). In dnsmasq.conf add the line:

  • 7
    Brilliant! Note for Mac users, it really is this simple: 1. sudo port install dnsmasq 2. edit /opt/local/etc/dnsmasq.conf 3. sudo port load dnsmasq – tomc Nov 16 '11 at 11:57
  • 19
    OSX with brew: same as above but brew install dnsmasq – Matt Humphrey Mar 7 '13 at 21:41
  • 2
    fyi brew > port (re @MattHumphrey suggestion) – electblake Mar 22 '13 at 2:57
  • 6
    Great tip. For an Ubuntu 14.04 desktop (which runs dnsmasq by default), create a file called /etc/NetworkManager/dnsmasq.d/dnsmasq-localhost.conf and put the line address=/localhost.com/ in it, then restart. – user38397 May 21 '15 at 18:48
  • 2
    How can I do this for multiple domains? And how do I refresh after editing the file? I want *.localhost and *.loc. I added two lines: address=/localhost/ and address=/loc/ For some reason only the first one works. And after commenting out both, and calling sudo service dnsmasq restart and sudo service network-manager restart I still have the *.localhost version working. – donquixote Sep 11 '17 at 22:33

It is not possible to specify wildcards in the /etc/hosts file. Either specify the required hostnames explicitly or alternatively set up a local name server with the appropriate rules.


You need to set up a DNS server and have each client use it for resolution. The server itself can be something as "light" as dnsmasq or as heavy as BIND.

  • +1 for dnsmasq, which is really well-documented and easy to use – David Schmitt May 2 '11 at 12:24
  • dnsmasq is not able to do this. – DisplayName Dec 13 '15 at 18:51
  • What part of the behavior described in the --address= section of the dnsmasq.conf man page doesn't cover the use case describe above? – Gerald Combs Dec 14 '15 at 16:33

Simple Workflow (no need to install anything)

I personally like to create a PAC file for that and make my browser just use it.

Step 1: create a file e.g.: *.proxy.pac* somewhere (I use my $home folder)

Step 2: paste this code (example is with port 8000):

function FindProxyForURL(url, host) {
  if (shExpMatch(host, "*localhost")) {
    return "PROXY localhost:8000";
  return "DIRECT";

Step 3: Make your Browser use this PAC file.

Youtube Video for PAC & Firefox

Step 4: Now you can test your app by accessing: http://mysubdomain.localhost/

Step 5: Enjoy :)

  • 5
    Perhaps it is worth mentioning that this will only affect your browser. Other programs (like wget, will not get affected by this). That is not per se a problem, but perhaps it is better to mention this. – Willem Van Onsem Apr 24 '18 at 21:02
  • If you go the .pac route and you're implementing a node.js server, you will be surprised to see that req.url is now an absolute URL. This is done because the assumption is that you want to write an actual proxy server, but it is surprising if you came to this technique just as a way to stop adding /etc/hosts entries for debugging. FYI to those who might go the same road I did. – Tom Boutell Jan 2 '19 at 20:26

I've tidied up an old project of mine:



  • linux where avahi and python-avahi are installable
  • you're ok with .local domains (avahi doesn't support any other kind)

Advantages over using dnsmasq or the python dns proxy:

  • other avahi/bonjour users on your local network can resolve the aliases you create and announce to the network ( providing you're allowing access to port 5353 )

You cannot use a wildcard in /etc/hosts.

Have a look here for a good walkthrough on how to accomplish on OS X using BIND, the built-in but inactive DNS server, and Apache.

  • 5
    Apache has nothing to do with subdomains. – Anonymous Mar 2 '10 at 17:55

This DNS based solution worked perfectly in my case, without need to install anything : https://gist.github.com/fedir/04e60d679d5657d1f9f9aa10b3168282 (Mac OSX 10.9)


If you want to use dnsmasq with NetworkManager you can (or even must?) start dnsmasq from NetworkManager by adding


to /etc/NetworkManager/NetworkManager.conf. Then the dnsmasq config goes to /etc/NetworkManager/dnsmasq.conf or /etc/NetworkManager/dnsmasq.d/ resp.


Short answer:

Your /etc/hosts/ file won't let you use wildcards or port numbers. You will need to create one entry for each of your subdomain

  • 2
    What would let you specify port numbers? Not DNS AFAIK. – ptman Mar 3 '10 at 11:45

The short answer is you don't. The longer answer is you need to be clearer on what you desire to actually achieve, because there is perhaps either a better way, and a different way to achieve it.

For web-hosting (I've never seen it used otherwise) is done in DNS in combination with a virtual hosting aware web server. For more information on wildcard DNS records (Wikipedia), and an article Wildcard hosting with Apache and Bind for Linux using bind and Apache.

At worst, you could use a local DNS server I suppose.


A common task for this subject is to map directories to subdomains. A very straightforward way for that is to append the directory-based entries automatically to the hosts file:


import os

hostsFile = open("/etc/hosts", "a+");

lines = hostsFile.readlines()

for fileName in os.listdir('/opt/subdomainDirs'):

    entryExists = False
    for line in lines:
        if fileName in line:
            entryExists = True  

    if not entryExists:
        hostsFile.write(" " + fileName + ".localhost\n");

Thank you tschundeee for what I consider to be the ultimate answer to this issue, wish I could just comment but here is the total configuration for those trying to accomplish the original goal (wildcards all pointing to same codebase -- install nothing, dev environment ie, XAMPP)

hosts file (add an entry)

file: /etc/hosts (non-windows)   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

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";

dnsmasq worked for me, except I had to make some additional steps.

Here is the full procedure:

  1. Prepend /etc/resolv.conf with the following line

  2. Add the following lines to /etc/dnsmasq.conf

  3. Restart dnsmasq (do not simply tell dnsmasq to reload the config files)

  • 1
    This will not work if your distro uses netplan (ubuntu 18.04 I'm looking at you) – MrMesees Feb 26 '19 at 5:15
  • That's sad. Just found out netplan was first released in July 2016. I'm sure their way of doing it is "better." – jbangerter Aug 25 '20 at 0:44

Copying from this blog here is how to do it on mac:


~ brew install dnsmasq

~ vim /usr/local/etc/dnsmasq.conf

# This file will be added to the configuration

~ vim /Users/your_user_name/.dnsmasq/dnsmasq.conf

# example.localhost will be resolved as, including subdomains

~ sudo brew services stop dnsmasq

~ sudo brew services start dnsmasq

We need to tell macOS to use as the first DNS resolver.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.