Make good use of your htaccess file

Posted on October 1

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.

Here goes:

  • 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:
ErrorDocument 404
ErrorDocument 401 /admin/authorization.htm
ErrorDocument 403 /password/forbidden.htm

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!

  • Redirects
    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:
Redirect /temp

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:

Redirect old file/folder new file/folder

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 instead of or, you will want to make this setting on your root htaccess.

    # .htaccess primary domain to subfolder redirectRewriteEngine on

    # Change to be your primary domain.
    RewriteCond %{HTTP_HOST} ^(www.)?$

    # Change ‘subfolder’ to be the folder you will use for your primary domain.
    RewriteCond %{REQUEST_URI} !^/wp/

    # Don’t change this line.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    # Change ‘subfolder’ to be the folder you will use for your primary domain.
    RewriteRule ^(.*)$ /wp/$1

    # Change to be your primary domain again.
    # Change ‘subfolder’ to be the folder you will use for your primary domain
    # followed by / then the main file for your site, index.php, index.html, etc.
    RewriteCond %{HTTP_HOST} ^(www.)?$
    RewriteRule ^(/)?$ wp/index.php [L]

    Now click on this link 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.
DirectoryIndex file.htm

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:
order allow,deny
deny from
deny from 18.132.152
deny from 24.2
allow from all

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 and 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:
order allow,deny
allow from 123.456.789.012
deny from all

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.

Tags: , // Category: Webmastering.


Comments are closed.