Use .HTACCESS to block IP Address and Disable Hotlinks

- Tuesday, July 12, 2016

A .htaccess file is a configuration file to setup web servers running the Apache Web Server. You can use it to setup basic redirect functionalities like a 404 file not found error occurs, or for more advanced functions such as content password protection or image hot link prevention.

Use .HTACCESS to block IP Address and Disable Hotlinks

The .htaccess file is used to alter the configuration of the Apache Web Server to Enable and Disable additional functions and features that the Apache Web Server has to offer.

Here in this blog I will show you how to use the .htaccess file to block unwanted IP Address which are spamming your website and eating up the bandwidth. In general a valid IP should be in the format (xxx.xxx.xxx.xxx) where "xxx" is a number between 0-255. You can enter a partial IP address to ban all IPs that contain this partial IP address within it.

Block IP Addresses with .HTACCESS file

Add the below code to your existing .htaccess file and upload to the root web directory:

# Block IP Address
<Limit GET POST>
 order allow,deny
 deny from 192.168.1.1
 deny from 122.171.24.
 deny from 46.119.
 deny from 69.
 allow from all
</Limit>
  • 192.168.1.1 (Blocks a specific IP address)
  • 122.171.24. (Blocks all IPs within the range 122.171.24.xxx)
  • 46.119. (Blocks all IPs within the range 46.119.xxx.xxx)
  • 69. (Blocks all IPs within the range 69.xxx.xxx.xxx.xxx)

Be careful when banning entire ranges of IPs, as you may also be blocking legitimate traffic!

Disable Hotlinking

Hotlinking is a term used on the Internet that refers to the practice of displaying an image on a website by linking to the same image on another website, also known as bandwidth leaching. Hotlink protection can save you lots of bandwidth by preventing other sites from displaying your images.

Use the below .htaccess code to disable hot linking for common file types like (.jpg, .png, .gif, .css and .js) from other sites, so only your own domain(s) are allowed to reference and access them.

# Disable HotLinking
RewriteEngine on
# Options +FollowSymlinks
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?example\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?YourOtherDomain.com/.*$ [NC]
RewriteRule .*\.(jpg|gif|png|css|js)$ /images/troll-face.bmp [L]

Click on the link to learn more about how to utilize .htaccess file efficiently.

Joydeep Deb

Marketing . Innovation . Technology

Share:

About the Author
Joydeep Deb

Joydeep Deb is a Senior Digital Marketer and Project Manager with strong experience in Digital Marketing, Lead Generation, Online Brand Management, Marketing Campaigns, Project Management, Search Engine Optimization (SEO), Search Engine Marketing (SEM), PPC, eMail Marketing, Web Analytics, Web Technologies, Web Design and Development.

With an MBA in Marketing. IIM Calcutta Alumini. Lives in Bangalore, Karnataka - India.

Get social with Joydeep at Twitter | Facebook | LinkedIn

Subscribe to Newsletter

Receive my latest posts right in your inbox? Enter your email address below to subscribe.

We'll never share your email with anyone else.