I recently wanted to point all subdomains for a test domain, let's say example.com to the localhost. Is there a way to point all requests on *.example.com to resolve to 127.0.0.1
5 Answers
It happens that /etc/hosts
file doesn't support wild card entries.
You'll have to use other services like dnsmasq. To enable it in dnsmasq, just edit dnsmasq.conf
and add the following line:
address=/example.com/127.0.0.1
-
6
-
2
-
5
-
7For macosx setup passingcuriosity.com/2013/dnsmasq-dev-osx is pretty helpful.– aamirJun 8, 2015 at 15:02
-
5rahilwazir, actually, it is if you dont want all subdomains. # also works as a wild card. I'm personally using `address=/dev#.example.com/127.0.0.1 to make dev123.example.com etc work.– Ray FossOct 28, 2015 at 13:29
use dnsmasq
Assuming that you're using a Debian-based dist(ubuntu, mint..), check if it's installed with
(sudo) systemctl status dnsmasq
If it is just disabled, start it with
(sudo) systemctl start dnsmasq
If you have to install it, write
(sudo) apt-get install dnsmasq
To define domains to resolve edit /etc/dnsmasq.conf
like this.
address=/example.com/127.0.0.1
to resolve *.example.com
! You must reload dnsmasq to take effect for the changes !
systemctl reload dnsmasq
Here is the 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)
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 [email protected]
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";
}
-
Does not works for hosts file on Windows. It will only accept www.example.com Apr 22, 2015 at 19:13
-
1this does work on windows using the pac file in firefox. I have tested this on multiple windows machines. the hosts file only requires the one entry, the pac file handles the subdomains. Apr 23, 2015 at 20:02
-
2The original question as I understand it is not about apache at all but on the networking level so I think your response misses the point :/– enTropyFeb 20, 2017 at 14:26
-
for using the pac file in internet explore see:campus.barracuda.com/product/websecurityservice/article/WSS/…– user1000456Aug 16, 2017 at 6:21
-
1Please note that when using a pac file to proxy requests to a host (e.g. localhost), a web server like Apache would use the full URL (including protocol and hostname) in REQUEST_URI variable instead of only the path component. Oct 15, 2017 at 14:33
It was a simple question, guys. The answer seems to be "no."
The reason I'm looking for information about this topic is that Google just added lots of new top level domains that seem to be designed for abuse, like .zip and .mov. They only went live five days ago and malicious actors have already set up pages using those domains to steal credentials, install malware, and worse. Leaving aside questions about Google's motivation, it would be very helpful to have a fast and simple way, that doesn't involve either going to some dubious web page to install someone's shiny new security app, which we rilly rilly swear doesn't have any hidden spy features, or spending a decade learning regex fu, to blacklist everything on all of them. For that matter, .biz has existed for twenty-two years and I have yet to see the first legitimate thing that uses it. I'd also like to bitbucket all traffic in either direction for .ads, .adult, .ru, .cn, .kr, .xyz, .za, and .zn. Anything and everything, for that matter, that doesn't use one of the original seven .tlds:
.com .edu .gov .int .mil .net .org
has been 100% sketchy AF since day one and it would be ideal for me, in my own personal use case, on my own computers, to point all of it to 0.0.0.0.
-
Hi and welcome here. You are right, spoofing is Evil, but I dont know any way to avoid it. Each time a nTLD appears :(– PhilippeMay 18 at 10:20
Using wildcard for host file is not possible or require complex method.
Go here to get ip address you wanna block https://dnsdumpster.com/
enter domain: example.com then roll down. Click on download xlsx of host. All ip of domain and subdomain is listed here.
Copy ip address from excel column to a txt file name ip_address.txt
First, you need enable firewall:
sudo ufw enable
Second, using this command:
while read -r ip_address; do sudo ufw deny out to $ip_address; done < ip_address.txt}
Note that you need use "out to" to block outbound connect from your pc to ip. If you use use "from" it only block inbound connect from ip to your pc and user still can get access to ip.
Third, test if ufw work by using:
sudo ufw status numbered
When you want remove a rule:
sudo ufw delete <rule-number>
-
-
point all requests on *.example.com to resolve to 127.0.0.1 = blocking all subdomain of example.com !!! May 14 at 13:35
-
The question doesn’t mention blocking, that’s your inference. Consider a site like tumblr.com. Each user gets their own subdomain (example.tumblr.com). When doing development work to test that feature, you’d need to resolve *.example.com to 127.0.0.1 so that you could test out different_usernames.example.com and make sure that your server-side code picks it up.– QuentinMay 14 at 14:57
-
Sorry for the misunderstanding. I had tried using dnsmasq but it not work on linux-ubuntu as expected. So if you need to resolve *.example.com to 127.0.0.1 for testing purpose you may need to follow other guide. If you need to effectively block all connection to a domain and its subdomain, you may considering using my guide. Tks May 15 at 2:31
-
You can also go to dnsdumpster.com to get list of all subdomain. It's just a matter of copy and paste. Host file need resolve subdomain to a specific IP before transfer to 127.0.0.1. That's why using wildcard is not possible in host file. May 15 at 3:05