April 25, 2007
.htaccess and mod_rewrite primer
Note: To make this easier for you, here are direct links for Part 2 and Part 3 of this brief .htaccess tutorial.
I've been meaning to do this forever because I often get questions about how to construct mod_rewrite statements to do redirects and such. It's such a powerful tool with so many possible permutations that it's tough to break it all down into simple language. I'm going to give it a whirl though, if for no other reason than to give folks a starting point of reference.
First things first, let's cover some basics.
When I talk about using mod_rewrite, or even mod_alias, I'm talking specifically about doing so with sites that are hosted on a Unix/Linux (often referred to as simply *nix) environment that is running Apache. I don't even dabble with Windows servers anymore, so if that's what you've got you'll need to find help elsewhere.
Let's talk just a bit about what mod_rewrite is all about. The below assumes your server has mod_rewrite installed and enabled. If it's not, you'll need to have a chat with your host.
Basically mod_rewrite is a way to tell a server how you want it to handle certain situations. It can be as simple as instructing the server to merge together the www and non-www versions of your site so that visitors and spiders alike can only reach one version or the other. Or it can be as simple as telling the server to tell all visitors to a page that has been removed that they can find it in a new location.
There are also some quite complex things you can do via mod_rewrite.
Normally when I'm discussing mod_rewrite I'm talking about using it in a .htaccess file that is placed at the root level of your site's web space. This .htaccess file (yes, it's really dot followed by htaccess) is the standard Apache location to put such instructions.
For the record, a .htaccess at the root level will have the ability to affect every page/file on a site. You may also place a .htaccess in lower level directories if you need something special done at a lower level but not at the root level. However this is a little atypical.
Also note that you may or may not be able to see the .htaccess file when you log into your site via FTP. The reason for this has to do with how your host has the FTP handler on your server configured. Normally Dot files (files that start with a period as the first character in the file name) are Hidden files. This is because normally they are sysetm files that people shouldn't be editing anyway, unless they know what they're doing.
Because I run my own servers and because I'm basically one of 3 people who have any access via even FTP I have my FTP set to show Dot files. If your server isn't configured this way you may have to use a special command to see the file. This special command is ls -al
When you're creating an .htaccess file you'll want to take care to use a plain text editor. For example, if you're on a Windows PC you'll want to use Notepad or something similar, not Word. You don't want anything to insert special characters, so use a plain text editor.
More basics...
When a server reads an .htaccess file it reads and processes it from top to bottom. Thus when you have more than one rule that might apply you need to put some thought into which you'll want to fire in which order. If you don't you could end up with conflicting rules or rules that don't perform the way you want them to.
Okay, so there are the basics. Next up we'll start to get into the different syntax you can use and what it does.
Note: To make this easier for you, here are direct links for Part 2 and Part 3 of this brief .htaccess tutorial.