Restrict Domains from Accessing a Site Using .htaccess

In one of my previous post, I discussed how to restrict specific IP and range of IP addresses from accessing your site. Luckily, the functionality of .htaccess for Apache Server doesn't stop there. It also allows web developers to block certain domain name from accessing or crawling their websites.

So today I will focus on this issue on this post and will show you the simplest method to block certain domain name from accessing your site. The snippet is fairly simple and shouldn't surprise you at all. Here you go.

order deny,allow
deny from
allow from all

Sounds familiar isn't it?

Partial or Sub Domain Blocking

Any given domain name may or may not have their sub domains (i.e. So just in case if you want to block the portion of the site or the sub domain, you can do that easily while allowing the actual domain (i.e. name to access your site. In such case you can use the following snippet.

order deny,allow
deny from
allow from

However, what you need to remember is that our default state (on 1st line) is "allow" here. So basically any other domain name would be allowed to access your site except

Similar ways, if you want to allow only the sub domain to access your site and block others, following method can be used.

order allow,deny
allow from
deny from all

Like before, "deny" is our default state here. So is only allowed to access our site and rest of the domain name would be blocked. Read more from Apache website on this issue.

Domain by IP Address

Now you definitely shouldn't forget that every single domain name (i.e. has their own IP address. If you want to block them by IP addresses, that's still valid and should work without any question. I already wrote a post on blocking IP addresses. Feel free to check it out if you are interested.


Commenting is disabled.