2007.09.09

.htaccess files are one of the most underused and underrated forms of web development. They are very easily overlooked or dismissed as unimportant by the amateur programmer. The reality of it is that .htaccess has some of the coolest and ridiculously easy features to offer.

The first thing I should mention is that .htaccess files can only be used on servers running apache.
No Windows IIS, sorry.

So, first things first. What the heck is an .htaccess file and how can it be used. Well .htaccess, or Hypertext Access, is the default name of Apache's directory level configuration file. It offers the ability to do things like change the default 404 (page not found) error message, password protection, SSL, SSI, ban ip addresses, default page, redirect and tons more. Most of this can be done using regular php or another server-side scripting language, but it is harder to implement and will take more time than a couple easly lines inside an htaccess file. 

The first trick to starting with .htaccess files is creating one. WindowsXP will not allow you to create a with a file extension but no filename, at least not using the gui. There are a couple different methods to getting an .htaccess file on your web server.

  1. Create a file then rename it using the Command Prompt with the rename function.
    1. rename c:\file.htaccess .htaccess
  2. You can upload the file onto your web server than use a FTP program to rename the file.

I'm sure there are many more methods but these two should get anyone up and running without much hassle.

Let's get started with what you can do.

 

Error Documents 

ErrorDocument 404 error-404.html

That's it. Stick that line into an .htaccess file in your root directory and every 404 error that a browser triggers will be directed to error-404.html
Here is a list of HTTP Status Codes , the error numbers you see like 404 or 403. This way you can make error pages for the most frequented error pages.

Default Page for Directory

 DirectoryIndex newhomepage.php

This will change the default page from index.htm/html/php or default.html, etc etc, to newhomepage.php

Blocking IP addresses or ranges

 order allow,deny
 deny from 152.100.52.54
 deny from 152.101.52.
 allow from all

This will block 152.100.52.54 and 152.101.52.* range from accessing files. With this you can block disruptive or malicious users as well as blocking an entire ISP from accessing your site.

Redirection 

# Redirects users from oldfile.html to newfile.html
Redirect /location/oldfile.html /location/newfile.html

Obviously, the redirect command sends the browser to another location. Useful for when you change hosts or domains and want to send users who visit the old site to your new one. I also included a comment in the first line to show what was going on. Comments are declared by using # symbol followed by text.

ModRewrite

 RewriteEngine On
 RewriteRule ^(index)(/.*)?$ index.php


My Favorite. Using the rewrite rules and regular expressions you can make sites that have url's like mine and others. By that I mean the url has no extension at the end and a large amount of directories, or so it would seem. The reality is I have a RewriteRule similar to this one here.

This takes any url that begins with index and directs the info to index.php. So now index.php is handling the url exactly as it looks. For Example:

mikebernat.com/index/page/articles will be sent to index.php but as you can guess, $_GET variables will be empty because the url does not have its usual ?variable=value  look to it. There is an easy way to fix this though to keep your pages that use $_GET variables compatible with your newly updated url format.

If your URL looks something like this:  http://www.mikebernat.com/index/page/article/id/21234
This code will create these variables:

$_GET['page'] = "article"
$_GET['id'] = "21234"

This is important because my urls used to look like http://www.mikebernat.com/index.php?page=article&id=21234
So by putting this snippet of code at the top of each one of my pages I can use the new style of URL while preserving my use of $_GET variables. 

 

There are a lot of different things you can do with .htaccess files, many of which are not even mentioned in this article. Each of these functions have many options associated with them. These files offer flexibility that is hard to match with other server-side scripting languages and I definitely suggest looking into using an .htaccess method over any other solution for your project.

Extra Reading

 .htaccess Tutorial - A good introduction to the functionality that .htaccess files offer
 Comprehensive guide to .htaccess - A more in-depth guide, covering areas like SSI, site rippers, and MIME types.
 .htaccess - Wikipedia's page on .htaccess

Get my RSS Feed!

Comments

No comments yet. Be the first!

* Name
* Email (Will not be displayed)
Website