Sooner or later, any aspiring webmaster worth her salt is going to come into contact with the .htaccess file. The best response to this is to say, “On Guard!” It’s one tricky rascal of a file to deal with, particularly if you are on a Mac.
You’ll read that it’s powerful. True. And you’ll read that it’s easy. Also true. But that’s only in a limited sense. Among other things it’s easy to hose your whole site with it. I should know. I’ve done it twice in the brief life of this blog. The first time I knew instantly, and probably none of you saw it. The second time was Saturday, and for 18 hours it slipped under my radar. Horrors!
I knew I’d altered the .htacess file and I knew I was playing with fire, but I also double-checked and thought all was well. Wrong, wrong, wrong. The home page was fine. Everything else disappeared, while I blithely went to a wonderful oh-so-Nashville concert and got a good night’s sleep. Yesterday I awoke to an email from a dear friend politely inquiring about the state of my blog. I knew in a trice what the problem was. The dreaded .htaccess file. Fortunately, I was also able to fix it in a trice.
I’ll tell you why I was monkeying with the .htaccess file in an upcoming post, but before that, I think a very brief discussion of what it is, why it matters and why it’s such a slippery beast is in order.
What The Heck Is An .htacess File?
First off, it’s an Apache thing. Most of you (whether you realize it or not) probably are using servers running Apache. If your server uses some other software (the most likely alternative being Microsoft’s IIS), you may happily ignore this post.
Second, even if it is Apache, depending on how the sysadmin (system administrator) has configured your server, you may or may not be able to use it. Usually you can, but in highly secured environments you can’t.
Sysadmins can control the things you do with Apache in other ways, which are more efficient and safe than .htaccess. However, most sysadmins aren’t at the beck and call of your average webmaster. If you are using an inexpensive host (a likelihood for most church sites), then you won’t have easy access to the sysadmins, but chances are they will let you use .htaccess.
So now, with those caveats aside, the .htaccess is a text file that controls the configuration of all the files in its directory and child directories, unless the child, as growing children are wont to do, sprouts its own .htaccess file with a countermanding directive.
Most often, I alter an .htaccess to route users to a more friendly page-not-found message than the standard, bleak 404. I also have altered it to control password access to a portion of a website and to speed up response time.
Sample .htaccess Commands
Now for some examples of the commands you can add to your .htaccess file. These snippets of code take up one line each of the file. All you have to alter are the strings in italics (and be sure not to use the quotation marks).
To Disable Directory Browsing (a good thing to do)
“Options All -Indexes”
Change the Default Page
“DirectoryIndex myhome.htm index.htm index.php”
Redirect to a New Page
“Redirect oldpage.html http://www.domain.org/newpage.html”
Redirect to a New Directory
“Redirect /olddir http://www.domain.org/newdir/”
Redirect to a Customized Error Page (another good thing to do)
“ErrorDocument 404 /notfound.html”
Pretty cool to be able to do all of those things, huh? But….
Why .htaccess Is So Crazy Making, Especially On a Mac
First notice that this file name is in fact only a file extension. Nothing precedes the period – and the period is crucial. Upload a file named “htaccess” (instead of “.htaccess”) and it won’t do a thing.
On a Mac, this means you normally won’t be able to even see the file, much less create it. One way Apple protects its machines from user error is to hide system files, including ones that begin with a period. I usually get around this by using either TinkerTool or the “File / Open Hidden” command in BBEdit. Oddly enough, Dreamweaver can see the .htaccess file locally on my machine, but not remotely on the server. For that I use Cyberduck.
However, not only is the .htaccess file hard to find for many of us, it’s hazardous. It turns out you can do much worse things than I have done, such as set up infinite loops. Read some of the official documentation for Apache 1.3 if you’d like a sobering experience.
Also, unbeknownst to you, programs you use like WordPress may be altering your .htaccess file. If you overwrite them, your site will stop working. That’s what I did Saturday. I accidentally overwrote a WordPress directive.
Precautionary Measures
To avoid the pitfalls, there are four things to do.
First, on your server find the .htacess file in the appropriate directory of your site, download it and back it up before you alter a thing.
Second, be clear on your environment. Know not just if you are running Apache, but what version. And be sure to track down your host’s documentation about .htaccess. It can be a goldmine. See, for example, my host’s documentation.
Third, read up on a particular command before you upload a change to the .htaccess. It’s kind of like getting a second opinion for an illness.
Finally, don’t just double check after you upload an altered .htaccess file. Triple check. Try it in another browser and test several pages in different subdirectories.
While I don’t mean to frighten you unduly with talk of illness and hazard, precaution is the key. Using .htaccess wisely can do many great things for your site that you wouldn’t be able to do otherwise, so get to know it and handle with care.
6 Comments »