.htaccess Loves You…

March 1, 2009

Chances are that if you have been working with WordPress over any measurable amount of time, you have encountered .htaccess.  Perhaps you were trying to configure permalinks?  Maybe you were trying to secure a directory?  Regardless, .htaccess gives you a large control over your site and your WordPress user experience.  In my opinion, a rudimentary understanding of .htaccess is crucial for bloggers and non-bloggers alike who self-administer their web presences.

Before we look at a practical application example involving .htaccess, let’s review what .htaccess is, what it governs and where it typically lives.  The .htaccess file is essentially a text file and, as such, can be edited by any number of text editors found out there today including notepad, ultraedit, etc. No big surprises there. The .htaccess file is usually found in the root directory of a webserver for a particular website (e.g. www.mysite.com/.htaccess). The location of the .htaccess file is important because it will affect what .htaccess has control of. In general, the .htaccess file will exert control over and cascade below to any directories and files under it. So, if you have an .htaccess in any directory underneath your root directory, the .htaccess file not have control or affect any directory or file above it in the file system. 

Next, let’s look at a real world application of .htaccess. One of the greatest utilities of .htaccess can be found in the ability to ban an IP or a range of IP’s from your site.  As usual, whenever I edit a file, I like to make a copy of the original just in case I need to revert back for any reason.  Usually, I like to rename files by adding the suffix:

.bak

or

.old

Use whatever naming convention makes most sense to you.

Moving on, let’s assume that the IP you want to deny access to is 123.456.789.0.  Open up your .htaccess file in your text editor of choice and use the following commands – one per line, please.

## USER IP BANNING
<Limit GET POST>
 order allow,deny
 deny from 123.456.789.0
 allow from all
</Limit>

NOTE: Two comments on this particular exercise:

  • I am always one to want to comment my code.  Makes for easier maintenance in the future.
  • One command per line, so make sure your text editor does not have a wordwrap feature toggled while you are editing.

Now, let’s go one step further with our example.  Let’s suppose that you have a _range_ of IP’s that you want to ban.  Personally, I’ve never run in to this particular scenario; however, you may have the need, so we will cover the potential below:

## USER IP BANNING
<Limit GET POST>
 order allow,deny
 deny from 123.456.789.
 allow from all
</Limit>

Notice the command to deny our sample IP is now missing the 4th quatrain of the address, in this instance, the ending “0″.  This command will now tell your site to refuse any connection from an IP with the prefix of:

123.456.789

Please remember that this is a small example of the power and utility of the .htaccess file.  I would recommend that, if you are interested in the topic, you refer to the official documentation on Apache Directives found on the Apache site.

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

Comments

Got something to say?