Basic Guide to .htaccess

26th September, 2008 by

By using a .htaccess file, you can configure certain elements on your site on a per-directory basis. though the file starts with a period, it is not appended to a file name, .htaccess is the file. Some examples of what can be done with a .htaccess file are:

NOTE: Any command that you place in .htaccess file will affect its current directory where it is placed and also its sub-directories. If you put a .htaccess file in the root folder it will affect the whole site.

Creating a .htaccess file

A .htaccess file can be created and edited in any text editor and then uploaded to the desired location, or you can even create it in your SSH command line environment making changes on the fly. We will document the SSH method.

  1. Connect to your account via SSH (if you are not sure how to do this, please see the SSH documentation).
  2. Navigate to the directory where you wish to create your .htaccess file (specific SSH commands can be found in the SSH Commands documentation).
  3. (Optional) You may wish to do a directory listing to see if a .htaccess file already exists in the directory you want. Because of the nature of .htaccess files, the default configuration of your SSH environment will not show it with a simple directory listing command, you will need to use the following command followed by the enter key:

     ls -al

    This will display ALL files in the current directory and will show you if you already have a .htaccess file.

  4. To create and/or edit your .htaccess file, use the following command followed by the enter key:

     nano .htaccess

  5. This will open up the nano text editor and load/create the .htaccess file. You may now make any edits or additions to your file.
    NOTE: If nano does not work on your account, you can use pico instead and it should work fine.

  6. When finished, press Ctrl+X to exit. You will be prompted to save your changes, follow the instructions to make the changes permanent.

Web site redirection (all or just a portion)

To redirect some or all of your Web site to another location, you will need to add the following lines to your .htaccess file (where the .htaccess file is located depends on what parts of your site you are redirecting):

 Redirect permanent /

The above code will permanently redirect your entire site to the address You would need to place that in the main Web folder to be effective though. If you only wish to redirect certain portions of your site, you can do that with the following command:

 Redirect 301 file.html

This code says to automatically redirect anyone trying to access to the specified address As you’ll notice, it’s important to put the whole URL in the second spot telling EXACTLY where to redirect to. You can also make that link and external link (links to a page not on, but just remember that it too has to include the EXACT URL (including http://etc…).

There are other ways to accomplish these types of redirection (such as META refreshes), but most are much more clunky and can negatively affect your search engine optimization, so learning and using the above method for redirection can really pay off big for your Web site.

Force Web site to always show www. in the address bar

If you would like to force your Web site to load with the www. at the beginning whether your site visitor typed it or not, you can accomplish this through the following .htaccess command:

 RewriteEngine on
 RewriteCond %{HTTP_HOST} !^$
 RewriteRule ^(.*)$$1 [R=301]

You will need to make sure you replace with your actual domain name.

Force SSL connection (require https://)

You can force your Web site (or a portion of your Web site) to be accessed securely (i.e., require https://) by creating an .htaccess file in the appropriate location that includes the following mod_rewrite rules:

If you have your own SSL certificate:

 RewriteEngine On
 RewriteCond %{HTTPS} off
 RewriteRule ^(.*)$$1 [R=301,L]

Important note: must match the domain name on your SSL certificate. If your SSL certificate has been issued to, for example, then you will need to use in the RewriteRule. If you have a wildcard SSL certificate, you may use %{SERVER_NAME} in the RewriteRule in place of the domain name.

If you are using WestHost’s Shared SSL:

 RewriteEngine On
 RewriteCond %{HTTP:X-Forwarded-Host} !^$
 RewriteRule ^(.*)$$1 [R=301,L]

Make sure to replace with your actual domain name.

Allow directory listing of files

If you would like to allow visitors to view the contents of a directory that does not have a default index.html page, you can accomplish this with the following .htaccess command:

 Options +Indexes

By default, this is disabled due to security purposes, so only turn on directory listing if you really need it and understand the risks involved.

There is so much more you can do with a .htaccess file, but the above is just a general guide to help get you started. For more in depth information on .htaccess files, please see the Apache .htaccess Documentation.

(Visited 1 times, 1 visits today)