.htaccess for beginners #1 - the basics

Always wanted to know what are those .htaccess files that keep coming up in lots of blog tweaking tips? You'll find out right now in the first part of this mini-series.

Technorati Tags: ,

What's a .htaccess file?

In case you don't have access to the configuration of your blog's webserver, the hosting company often allows you to still modify some of the server's configuration settings with .htaccess files.

These are simple plaintext documents that you can upload to your web directory, and contain various Apache webserver commands that affect the behaviour of your site.

Note, that the name of these files is ".htaccess", so it's not a file name extension like .html or .doc. When uploading these files through FTP, make sure that you set the transfer mode to ASCII - binary transfer usually won't work with Windows or Mac files.

When to use .htaccess files?

These files are usually advised when you don't have direct access to the webserver configuration files. Since they affect the performance of the server, you should only use them when you don't have other choices.

It's also OK to temporarily use .htaccess files to finetune some configuration settings, since they don't require the restart of the webserver. But don't forget to move your polished configuration into the apache config files once it's done.

It's important to know that not all hosting companies allow the use of .htaccess files - ask them if you have doubts.

How to create .htaccess files?

Just make a plain text document in your favourite text-editor (Notepad on Windows and SimpleText on Mac is OK), enter the configuration directives (see example below) and save it as .htaccess. Upload it to your server's webroot directory (unless advised otherwise), and check your blog - you should immediately see the results.

What can I do with .htaccess files?

Lots of things - that's what this series will be about. I have a basic example here for starter: creating a custom error document to your blog. This will appear to your visitors whenever they try to load a page that doesn't exist on the server - instead of the default ugly error message.

First of all, create an HTML page in the design of your blog explaining that the document your visitor is looking for is unfortunately not available. Save it as errorpage.html and upload it to your webserver's webroot directory. The filename doesn't matter, just choose something descriptive that you'll remember later.

Second, create a .htaccess file as described above, with the following contents (just one line):

ErrorDocument 404 /errorpage.html

Save it, and upload to your server's webroot. In case you already find a .htaccess file there, you can add the above line to this file.

After you're done, try to load a page that doesn't exist in your blog, sit back, and smile. (If you don't smile, doublecheck that you didn't forget to upload the file in ASCII mode, and the useage of .htaccess files is really allowed on your blog.)

Next time I'll look into other tricks with .htaccess files - subscribe to our feed if you wouldn't like to miss it.

March 30, 2006

Submit to del.icio.us | Digg | Furl | reddit | Y! MyWeb | comments (0)

Post a comment