Wildcards in a Windows hosts file - windows

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?

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:
Start
Programs
Acrylic DNS Proxy
Config
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:
Start
Programs
Acrilic DNS Proxy
Config
Restart Acrylic Service
You will also need to adjust your DNS setting in you network interface settings:
Start
Control Panel
Network and Internet
Network Connections
Local Area Connection Properties
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.

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

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 6 years old and not correct anymore.

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.

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

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.

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

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/

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.

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/

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

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"
}
]

#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

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

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.

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.

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

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

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

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.

Related

I cann't call my localhost by ip address

I am trying to call my xampp localhost from another computer's browser, I have changed host file at C:\Windows\System32\Drivers\etc by appending (192.168.1.105 localhost) at the end this file and save it and then restart computer, but I could not call my localhost from another computer.
Thanks
You need to access port forwarding in your router and forward port 80 only as TCP. Also, the host file code should look something like YOUR IP YOUR IP and not YOUR IP localhost. Furthermore, you need to edit httpd-vhosts.conf from apache folder in xampp instalation with something like that
<VirtualHost *:80>
ServerAdmin whatever#whatever.com
DocumentRoot "PATH TO YOUR FOLDER"
ServerName YOUR IP
ServerAlias YOUR IP
</VirtualHost>
After that you must restart apache in XAMPP control panel (MySQL not necessary)
THE SOLUTION ABOVE WILL LET YOU SEE YOUR CONTENT FROM ANYWHERE AS LONG AS YOUR LOCAL PC IS TURNED ON AND HAVE INTERNET CONNECTION. THE SOLUTION BELOW (I DIDN'T TEST IT, BUT I GUESS IT WILL WORK) WILL LET YOU ACCESS YOUR CONTENT FROM LOCAL NETWORK ONLY.
edit your httpd-xampp.conf file as following and after that you should probably need to perform a browser cache cleaning
# New XAMPP security concept
#
<LocationMatch "^/(?i:(?:xampp|security|licenses|phpmyadmin|webalizer|server-status|server-info))">
Order deny,allow
#Deny from all
#Allow from 127.0.0.0/8
Allow from all
ErrorDocument 403 /error/HTTP_XAMPP_FORBIDDEN.html.var
</LocationMatch>
When you add 192.168.1.105 localhost on the other PC (the one you want to use to view your website), it will look for a website on the same machine. Delete the entry from the hosts file and enter the IP address in your browser and it should work if the webserver is running.

Issue with Virtual Host on Zend Server CE and Mac OSX

I'm using Zend Server CE 5.6.0 on Mac OSX.
My httpd.conf has the following line, adding the virtual hosts file:
Include conf/extra/httpd-vhosts.conf
At that file, I have the following:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName localtextil.drupal.lh
DocumentRoot /usr/local/zend/apache2/htdocs/Obiz/LocalTextil-Portal/drupal
</VirtualHost>
So, by definition, when I try to access localtextil.drupal.lh, it should redirect me to its document root.
The problem is, when I try to access that host, I got an error.
And when I simply access localhost, I go to the site located on /usr/local/zend/apache2/htdocs/Obiz/LocalTextil-Portal/drupal.
What am I doing wrong here?
Two things - first is that the virtualhost definitions (as I understand it) override apache's mappings between directories and servernames. The first defined virtualhost is what apache will use as a default when it can't find a map - so it should be a generic setup (reflecting your original domainname, in your case, localhost) first, then your specific setup following it.
Second is that if your folder is outside of where you have given apache permissions to read, then you may get an error. The directory permissions are set by a DocumentRoot directive - however:
Now when a request arrives, the server will first check if it is using an IP address that matches the NameVirtualHost. If it is, then
it will look at each section with a matching IP address
and try to find one where the ServerName or ServerAlias matches the
requested hostname. If it finds one, then it uses the configuration
for that server. If no matching virtual host is found, then the first
listed virtual host that matches the IP address will be used.
As a consequence, the first listed virtual host is the default virtual
host. The DocumentRoot from the main server will never be used when an
IP address matches the NameVirtualHost directive. If you would like to
have a special configuration for requests that do not match any
particular virtual host, simply put that configuration in a
container and list it first in the configuration file.
The Apache document page on name based virtual hosting is here, it should help give some ideas. Basically you need to look at the definitions you have for DocumentRoot and the servernames, and go through them making sure they are all "ok".

VirtualHosts does not work on Mac OS 10.7

I want to create VirtualHosts on Mac OS 10.7 and therefore I edited the /etc/apache2/httpd.conf. I uncommented the line "Include /private/etc/apache2/extra/httpd-vhosts.conf" to include the virtual hosts. In the file /private/etc/apache2/extra/httpd-vhosts.conf I wrote the following:
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "/var/www"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/someFolder"
ServerName myApplication.dev
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/someOhterFolder"
ServerName myApplication2.dev
</VirtualHost>
There were two example virtual hosts before which I deleted. In my /etc/hosts file I added the following:
127.0.0.1 myApplication.dev
127.0.0.1 myApplication2.dev
I restarted my Apache and typed myApplication.dev and myApplication2.dev in the browser but I get an error "server not found" and it makes www.myApplication.dev in the browser (the same for myApplication2.dev).
Did I forget something to configure? I activated PHP in httpd.conf, mysql is installed also, but that has nothing to do with virtual hosts, I think.
Thanks for your help!
apachectl has an option -S to check vhost.conf file syntax. You can find these lines in vhosts.conf file.
> # You may use the command line option '-S' to verify your virtual host
> # configuration.
So, when you run
sh-3.2# apachectl -S
if you get Syntax OK result it means that there is no problem in your vhosts.conf file.
httpd: VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:80 is a NameVirtualHost
default server zz.xxxx.com (/private/etc/apache2/extra/httpd-vhosts.conf:27)
port 80 namevhost zz.xxxx.com (/private/etc/apache2/extra/httpd-vhosts.conf:27)
port 80 namevhost yy.xxxx.com (/private/etc/apache2/extra/httpd-vhosts.conf:35)
Syntax OK
If conf file has any problem it will tell you error line(s) like
sh-3.2# apachectl -S
Syntax error on line 33 of /private/etc/apache2/extra/httpd-vhosts.conf:
CustomLog takes two or three arguments, a file name, a custom log format string or format name, and an optional "env=" clause (see docs)
make sure that your vhosts.conf file has true configuration.
I had the exact same problem using OS X Lion. I fixed it by adding "::1 myhost.dev" to /etc/hosts:
127.0.0.1 myhost.dev
::1 myhost.dev
Incidentally, the ::1 also fixes a bug that makes page loading very slow on virtual hosts served from the Mac.
Are you using an HTTP proxy? If so, make an exception for myApplication.dev and myApplication2.dev.
What I meant was that the problem "server mot found" means that your browser cannot find the ip adresses of the hosts "myapplication.dev". This may be because you're using an http proxy, possibly one configured by your hosting company. In any case, you don't even reach the server, so you never get to try the virtual host configuration at all.
To just try the virtual host configuration, you can use telnet in a Terminal window and talk HTTP directly to the server, like this:
yourmacbox:~ yourname$ telnet 127.0.0.1 80
You should see the following text:
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
Then you type
GET / HTTP/1.0
Host: myApplication.dev
Now, hopefully you should see some response from your web server. This shows that once you can connect to it, the virtual hosts things works.
I had the same problem, and noticed that the ServerRoot "/usr" was set as shown and incorrectly after the 10.7 upgrade. The httpd.conf file was still under /etc/apache2, but this setting in it was pointing to the wrong place. Once I had fixed that by changing to ServerRoot "/etc/apache2", all my previous virtual host configuration got picked up properly.
I also had to re-enable virtual hosts by uncommenting line 477 as mentioned here http://brettterpstra.com/fixing-virtual-hosts-and-web-sharing-in-mountain-lion/ That didn't quite kick in until I had fixed the path issue above.

Can't get my Vhosts back running after Mac OS X Lion upgrade

After upgrading my MacBook Pro to Mac OS X Lion I am not able anymore to get my 'Zend Server' running like before. At first the 'Zend Server' gave me servile errors by starting it from the command line. I fix this by doing a complete new installation of Zend Server.
After this new installation Zend Server runs fine and no problems at all. So now I want to put back my zend projects. I made a backup of my vhost file and put it back, it seems to be Lion didn't touch my host file so I didn't touch either (just checked). I did the follow things;
Put one of my vhosts back in conf/extra/httpd-vhosts.conf (just one to check).
Uncomment the vhost include in conf/httpd.conf
Checked my host file
Restart zend server from command line
After this I still cant reach the project by browsing to http://foo.localhost:10088/
Vhosts
<VirtualHost *:10088>
ServerAdmin my#email.com
SetEnv APPLICATION_ENV "development"
DocumentRoot "/Users/nicky/Zend/workspaces/DefaultWorkspace7/foo/public"
ServerName foo.localhost
ErrorLog "/Users/nicky/Zend/workspaces/foo-log"
CustomLog "/Users/nicky/Zend/workspaces/foo-log" common
</VirtualHost>
Hosts
127.0.0.1 localhost
127.0.0.1 dummy-host.example.com
127.0.0.1 dummy-host2.example.com
127.0.0.1 foo.localhost
I am not sure if this can have effect on the problem, but by restarting zend from the
command line, i get the follow:
MacBook-Pro-van-Nicky-Klaasse:~ nickyklaasse$ sudo /usr/local/zend/bin/zendctl.sh
restarthttpd: Could not reliably determine the server's fully qualified domain name,
using MacBook-Pro-van-Nicky-Klaasse.local for ServerName
Regards,
Nicky
I've had similar problems as well with that. I've been searching around for an answer, found some solutions but none of them seemed to work.
The only thing that finally made it working (not even sure how it actually helped), is going into system preferences -> sharing and enabling web sharing. That is normally starting your apache2 server but I wasn't using the default one, I am running another apache2 process with a different httpd configuration. Anyway, after starting it and stopping it (from the system preferences window), it seemed like it was working.
One thing that could be useful is checking your error_log as well as the access_log in /var/log/apache2/. Check the access_log to see what is the address trying to be resolved. At first, whenever I was making a request on my website through the Virtual Host, in my access log I only had 127.0.0.1 which is incorrect. After it worked, I can see that the requests are www.example.com and not the localhost, showing that the virtual host is indeed working.
Not sure if that will help you, give it a try, paste the access_log and error_log latest lines to see where there could be a problem.

Having a url of : 'example.com' instead of 'localhost:8888/drupal' [Mamp]

How can i run local tests ( with mamp ) that have urls of this kind : www.example.com instead of http://localhost:8888/install.folder
Thank you
If your tests are truely local, you may simply add an alias in your hosts file (/etc/hosts on linux). This alias will cause your computer to resolve www.example.com as 127.0.0.1 (localhost).
If you wish to use this outside of your computer, you will need to purchase a domain, and set its DNS up to forward to your IP address (and open the appropriate port in your router to forward to your computer).
If you don't want to have to include the port number (':8888'), change the port that your server is being hosted on to the default (80). This may be done through the server's configuration file by changing the 'Listen' directive.
If you do not want to have to add the '/drupal' path, add an 'Alias' directive to redirect the user from the root path ('/') to '/drupal'. You may also change the 'DocumentRoot' to drupal.
You will need to add an entry to your hosts file like this:
127.0.0.1 www.example.com
Note: this will not keep you from having to enter the port number or directory - it will only allow you to use a host alias for the host name itself.

Resources