123

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

1

5 Answers 5

127

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
10
  • 6
    for wildcard entry - address=/.example.com/127.0.0.1
    – vivex
    Mar 31, 2015 at 12:08
  • 2
    @Vivek Its not required Apr 23, 2015 at 12:10
  • 5
    For ubuntu dnsmasq setup see this one. May 13, 2015 at 21:13
  • 7
    For macosx setup passingcuriosity.com/2013/dnsmasq-dev-osx is pretty helpful.
    – aamir
    Jun 8, 2015 at 15:02
  • 5
    rahilwazir, 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 Foss
    Oct 28, 2015 at 13:29
11

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
8

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";
}
7
  • Does not works for hosts file on Windows. It will only accept www.example.com Apr 22, 2015 at 19:13
  • 1
    this 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
  • 2
    The 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 :/
    – enTropy
    Feb 20, 2017 at 14:26
  • for using the pac file in internet explore see:campus.barracuda.com/product/websecurityservice/article/WSS/…
    – user1000456
    Aug 16, 2017 at 6:21
  • 1
    Please 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
1

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.

1
  • Hi and welcome here. You are right, spoofing is Evil, but I dont know any way to avoid it. Each time a nTLD appears :(
    – Philippe
    May 18 at 10:20
-1

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>
5
  • The question says nothing about blocking anything.
    – Quentin
    May 7 at 13:08
  • 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.
    – Quentin
    May 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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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