A really cool feature that web designers overlook is the use of the .htaccess file. Simply put, whenever you see a custom error page (404, 403, etc.), use server side includes, restrict IPs, load a default page, or do any host of events invisible to the user, you are probably going to be using .htaccess.
.htaccess is a simple text file that you add and edit in a text editor such as Notepad. You save the file as .htaccess without any extra characters before or after the name of the file. I know the file’s name is a little strange. Then again, most cool things are. Here is how the file looks when uploaded to a web server:
Anyway, the following paragraphs will try to highlight some of the more useful .htaccess features and the code required to get those features to work. To test the following features, copy and paste the line(s) of code that you want into Notepad, save the file as .htaccess, and upload the file to the root web directory of your web server.
- Custom Error Pages
Don’t you hate it when people access your site via a bad link and are faced with an error message such as “Permission Denied” or “Page Cannot be Found”? Well, you can liven up the error messages by creating custom error pages for each type of error you anticipate your visitors will encounter:
To create custom error pages, replace the error code next to ErrorDocument to the appropriate error code, and change the URL of the page that will receiving the error-prone user!
Here is a nightmare scenario. You decide to shift an entire folder’s worth of pages and images into a new folder on your server. If you aren’t using any 3rd party programs to update any links to the new location, your users are going to be seeing a lot of the Custom error pages you may have used previously.A solution would be to the .htaccess file’s redirect feature. For example, if you are interested in redirecting users from a folder called temp to another folder, you would use the following code:
Where the first argument is the folder or filename that you are trying to redirect people from, and where the second argument is the file or directory you are trying to get people to go to instead:
The above line of code is a template for how the Redirect feature of .htaccess works.
- Advanced Redirects
In order to keep your hosting account tidy you may want to install your WordPress files and any other independent script each in a sub-folder. If you will prefer users to access your main site by typing http://allqoo.com instead of http://allqoo.com/wordpress/ or http://allqoo.com/wp/, you will want to make this setting on your root htaccess.
Now click on this link http://allqoo.com and you will see how this request be send to subfolder /wp/ automatically.
- Changing Home Page/Directory Page
When somebody types the URL to your site, more than likely, by default the index.htm or similar page will load first. You can actually use .htaccess and change which file gets loaded first when your URL gets loaded.
In the above code, replace file.htm with the name of your new default page. When your visitors visit your site by simply typing in the URL of your site, they will be taken to whichever the default page you specify in the place of file.htm in the above code.
- IP Blocking
If you would like to prevent an individual or a group of individuals on a certain IP range from accessing your site, you can selectively deny access to them.You would use the following command format:
Of course, you would probably not be interested in blocking the fictitious IPs I mentioned above. You should change those to something more relevant. If you are really not well liked, you can add as many deny from lines of code as you want.
If somebody from an entire IP range is bugging, you can block all IPs within that range by only entering a smaller portion of their IP such as 24.2. All IPs that being with 24.2 such as 188.8.131.52 and 184.108.40.206 will also be blocked automatically. You may want to be careful for you may not want to block access to visitors that you still want accessing site.
- Applying .htaccess to Folders and Sub-Folders
Your .htaccess file doesn’t need to be in the root directory, you can put it in any directory – it then applies to the directory you put it in, and all subdirectories.You can also allow only from certain IPs – for example:
Perhaps not so useful if you’re applying it to your whole site, but good when used with other directories.
The above .htaccess tricks are ones that I feel you will enjoy using. Remember, some server hosts may consider using .htaccess a violation of your privileges. Some HTML editing programs may have their own file management and access systems that do not play well when in the presence of .htaccess. Also, IIS servers do not support .htaccess.