Apache .htaccess File - What is it..? – What does it do..?
Assuming you followed my previous advice & your site is hosted on a Unix or Linux server which runs Apache, you may already be familiar with your .htaccess file. But for those of you that have not discovered the advantages of using this file, we will hopefully enlighten you in this article, by looking at some of the functions that this powerful little file can help you with.
In part B we will have an assortment of .htaccess files that will deal with mod rewrite, url redirect, allow access, block access etc. You may copy & use any of these files as your own .htaccess file, but first let’s take a look at the file itself & try to get a better understanding of this handy little tool.
What is the .htaccess file..?
The .htaccess file is a text file which should reside in your root directory and/or in any subdirectory of your website / blog. There can be only one htaccess file available in any particular directory, you may find that one already exists, or you may need to create one, depending on which directory you are working on. Any commands in a .htaccess file will affect both the directory it is in and any subdirectories of that directory. Therefore if you place one, in your main (root) directory, it will affect your whole site. If you place one for example, in wp-admin, it will affect wp-admin plus all subdirectories of wp-admin.
Some Important Points About .htaccess:
Any website or blog hosted on a Windows server does not use or recognise the .htaccess system. There will be procedures for achieving the same things that .htaccess can do, but it just isn’t as simple or as elegant as the way Apache manages things in my humble opinion! So unless you are on a Linux/Unix server, then I am afraid this article is of no use to you. Sorry.
Having FrontPage extensions installed will often produce a warning about editing or changing your htaccess file, at best the changes will not work and at worst it may make a complete mess of your FrontPage extensions. Therefore please follow these tips before proceeding;
1. Always keep a backup of the original file. If things go pear shaped, then simply upload via FTP to overwrite the file that caused the error.
2. Always make certain you have an upto date backup of your database. If replacing the htaccess file with the original, does not cure the problem, then it is very likely you have corrupted your database! Restoring your database using an upto date backup will cure this error and return everything to as it was prior to your editing your htaccess file.
3. Your host may not support alteration of the .htaccess file; either contact them first and ask before you make changes or proceed with caution and be sure you have a backup of the original file in case of problems.
Please understand that any changes you make to your htaccess files, you do so at your own risk – I cannot be held responsible for any harm or problems it may cause, due to typing errors or your failure to follow the above guidelines.
Working With Your .htaccess File:
Locating your htaccess file
Typical location of htaccess file for mod rewrite
Depending which version of WordPress you are using, you may have a problem finding it! When you FTP to your site the .htaccess file is generally the first one displayed in a directory if it exists. Some servers are configured to hide files whose names begin with a period. Your FTP client allows you to choose to display these files. In WS_FTP you can do this by entering -la or -al as indicated in the image on the left and then clicking Refresh. Other clients may use a different method – check the help files in yours.
Typical .htaccess file for mod rewrite in WordPress:
Editing should be done in a text editor, such as NotePad or NotePad++ which is a much better choice for editing all manner of website config files whether php, html, xhtml, xml etc. Never attempt to edit .htaccess files in editors such as FrontPage or MS Word as they are known to introduce errors in the form of special characters, which you will not see! The best procedure is to download a copy of your .htaccess file to your computer, edit it, and upload again, remembering to save a copy of the original in case of errors.
If you do not already have a .htaccess file you can create one in NotePad, it is just a simple text file. However you may encounter problems when saving it as there is no file name as such, .htaccess is the extension only & not the file name. This is one of the reasons I recommend using NotePad++ (download here) . If you are not sure how to create your own .htaccess file then you may (download here).
Note: You must upload and download your .htaccess file in ASCII mode, not BINARY. So please ensure that at the very least, your FTP client is set to auto detect transfer type.