.htaccess">Adventures in .htaccess
I have been completing CavLec’s move from textartisan with discoveries about .htaccess files. I thought I would share what I’ve learned. (Yes, I know I should have known all this for years. So I’m slow.)
Here’s the deal. First, your Mighty Sysadmin needs to be using Apache to run your website. Most do; assume as much until told otherwise. Second, your MS has to allow you to use .htaccess files. Some may not. If you don’t feel comfortable asking, just make an .htaccess file, upload it, and see if it works.
An .htaccess file is a plain text file. If you edit the code on your web page by hand, you can almost certainly use the same program to edit .htaccess. Don’t do it in a WYSIWYG web-page tool, and for heaven’s sake don’t do it in Microsoft Word!
You probably want one .htaccess file (simpler), and you probably want to put it at the top level of your website; you can affect the whole site or any part of it from there. You are, however, permitted an .htaccess file in each of your directories, so if that makes your life easier, do it.
The .htaccess file contains instructions to the web server, many of the same instructions that your Mighty Sysadmin can issue. (You can’t do everything your MS can, but you can do quite a bit.) When asked for a page in a directory with an .htaccess file (or a subdirectory of a directory with an .htaccess file), the web server checks the .htaccess file and executes any instructions that affect the page asked for.
What kind of instructions can you issue? Well, you can password-protect a page or a directory (though this is slightly more complicated), you can create custom error pages, you can map troublesome filename extensions to each other to avoid 404s—and, when you move stuff around, you can have the server redirect the old page to the new page. This is what I got into.
Here’s how it works:
Redirect permanent /path/to/old/file http://URL/of/new/file
Redirect is the command. permanent says that this is a permanent move, so don’t expect the page to reappear here. (You can substitute temp for a temporary move, or seeother for a different page that serves the same purpose. If the page is just plain gone forever, put gone and do not put the URL of the new file—though for my money you might as well let it 404.)
The path to the old file must start from the topmost directory of your website. So if I were to redirect the CavLec page (http://yarinareth.net/caveatlector/index.html), I would put /caveatlector/index.html. The URL to the new file must be the complete URL as you would type it in a web browser.
Pretty slick. Works like a dream, too; try this link to CavLec on textartisan and see where you end up. (By the way, permalinks to the CavLec archive on textartisan are currently 404ing; I hope to fix that shortly, but because of fragment identifiers and changing my main archive type it’s a mite more complicated. Regexes ho!)
There’s a good .htaccess tutorial on OpenSourcePan, and another one on JavaScriptKit.
The only really hard thing about this is grammar. Is it a .htaccess file, or an .htaccess file?