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 example.com 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. sub.example.com). 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. example.com) name to access your site. In such case you can use the following snippet.
order deny,allow deny from sub.example.com allow from example.com
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 sub.example.com.
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 sub.example.com deny from all
Like before, “deny” is our default state here. So sub.example.com 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. example.com) 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.