Knowledgebase: Administration

How to Block IP Addresses via an .Htaccess File

Posted by Justin Davis on September 01 2015 04:41 PM

An .htaccess (hypertext access) file is a configuration file that allows server configuration changes on a per-directory basis. The .htaccess file will override all global configurations for the directory in which it is located, as well as for all subdirectories. Common uses of the .htaccess file include: redirects, custom error pages, and PHP settings. Before making any changes to your .htaccess file, we strongly suggest making a backup copy of the file to allow easy restoration, if needed. This is because a single typo in the .htaccess file can cause your entire website to show an internal server error.

To create an .htaccess file, you can simply create a text document in an editor such as Notepad or Notepad++, and save the file as ".htaccess". Any of th examples below will be added to a new line in the file, then saved. Once your .htaccess file is created, you will need to upload it to the folder in which you wish for the file to take effect. If you are not familiar with uploading files to your server, please review the following articles: Where to Upload Site Files via FTP, Using an FTP Program, or How to Upload or Download Files Using File Manager in the Control Panel

This article provides two methods by which you can manage who is allowed to visit your site: either by blocking an IP, or allowing only specific IP addresses through.

  • Block single IP addresses from accessing your site: This will prevent the specified IP(s) from visiting your website. In this example, we have blocked IP address 101.41.163.101. Additional IPs can be added by duplicating the "deny from" line, within the opening and closing lines below.

Order Allow,Deny
Deny from 101.41.163.101
Allow from all

  • Allow only specified IP addresses to access your site: This is useful in cases where your site is under development and you don't want anyone but  yourself accessing it, or when you want the site to only be accessible from an internal network (such as an in-office application). The below example will allow only the IP address 209.41.163.23 through. Additional allowed IPs can be added by duplicating the "Allow from" line, after the two opening lines below.

Order Deny,Allow
Deny from all
Allow from 209.41.163.23

After uploading your updated .htaccess file to the web server, you will need to synchronize the changes for them to take effect. This is done via the customer control panel. Instructions on synchronizing the .htaccess file to your web server can be found here: How to Sync an Htaccess File to the Web Server

If you have any further questions that were not answered in this article, please contact us.

(1 vote(s))
This article was helpful
This article was not helpful

Comments (0)
Copyright © 2016 HostMySite