Let’s Have Some Fun With .htaccess

The Apache web server has some configuration options that are available to the server administrator. In a shared hosting environment, you don’t have access to the main Apache configuration, so you’re stuck with the default settings. However, it is possible to override some of the default settings by creating (or editing) a file named .htaccess . This htaccess tutorial will show you all the basics you will need in everyday usage.

The .htaccess is a simple ASCII text file placed in your www directory or a subdirectory of your www directory. You can create or edit this file in any text editor (such as NotePad) and then upload it to the directory for which you want to modify the settings. Be sure that the file is uploaded in ASCII (not BINARY) format, and be sure that the file permissions for the file are set to 644 (rw-r–r–). This allows the server to access the file but prevents visitors from accessing the file through their web browser (a security risk.)

Commands in the .htaccess file affect the directory that it’s placed in and all subdirectories. If you put the .htaccess file in your www directory, it will affect your entire website. If you place it in a subdirectory of your www directory, it will affect only that directory plus and subdirectories of that directory.

Most .htaccess commands are designed to be placed on one line. If your text editor wraps lines automatically, you should disable that function before saving and uploading your file. Also, note that .htaccess commands are case-sensitive.

Please note that some web hosts do not technically support overriding the default server settings. The information presented here may work and it may not, or it may work today and not tomorrow. Use it at your own risk.

Some of the things you can do with .htaccess include:

Customize Error Messages

If you want to override the server’s error pages, you can use .htaccess to define your own messages. This capability is discussed in the Custom Error Messages post. An example of the syntax is:

ErrorDocument 500 /error.html

Override SSI Settings

By default, only pages ending in the .shtml extension will parse server-side includes (SSI) on some servers. You can override this restriction in your .htaccess file:

If you want to override the default server configuration so that SSI will work with .html documents, you can create a file named .htaccess and upload it (in ASCII mode) to your main www directory. Add the following lines to your .htaccess file:

AddType text/html .html
AddHandler server-parsed .html

If you want both .html and .htm documents to parse SSI, create your .htaccess file with these lines:

AddType text/html .html
AddHandler server-parsed .html
AddHandler server-parsed .htm

Change Your Default Home Page

To browse your site by specifying the domain name only (e.g., https://www.hostingmanual.net) instead of having to specify an exact page filename (e.g., https://www.hostingmanual.net/filename.html), you must have an index page in your www directory. Default acceptable file names for index pages include index.htm,index.html,index.cgi,index.shtml, index.php, etc. Note that they’re all named index.

There is also a default order of precedence for these names. So if you have both a file called index.cgi and a file called index.html in your directory, the server will display index.cgi because that name takes a higher precedence than index.html.

Using .htaccess, you can define additional index filenames and/or change the order of precedence. To set your index page as hieronymous.html add the following line to your .htaccess file:

DirectoryIndex hieronymous.html

This will cause the server to look for a file named hieronymous.html. If it finds that file, it will display it. If it does not find that file, it will return a 404 Missing Page error.

To change the order of precedence, enter a DirectoryIndex command with multiple file names on the same line. The order in which the file names are listed (from left to right) determines the order of precedence. For example,

DirectoryIndex hieronymous.html index.cgi index.php index.html

Enable Directory Browsing

Due to security concerns, most web hosts will not allow directory indexing. This is the option that allows the contents of a directory to be displayed in the browser when the directory does not contain an index page.

For example, if you make an HTTP call to a directory such as http://yourdomain.com/images/, it would list all the images in that directory without the need for an HTML page with links.

If you require this option on specific directories, you can still set it. You can reactivate it by adding the following line to your .htaccess file:

Options +Indexes

Once you add this, the directory will fully index again.

Block Users from Accessing Your Web Site

If you want to deny access to a particular individual, and you know the IP address or domain name that the person uses to connect to the Internet, you can use .htaccess to block that individual from your website.

<Limit GET>
 order deny,allow
 deny from 123.456.789.000
 deny from 456.78.90.
 deny from .aol.com
 allow from all
</Limit>

In the example above, a user from the exact IP number 123.456.789.000 would be blocked; all users within a range of IP numbers from 456.78.90.000 to 456.78.90.999 would be blocked, and all users connecting from America Online (aol.com) would be blocked. When they attempted to browse your website, they would be presented with the 403 Forbidden (“You do not have permission to access this site”) error.

Redirect Visitors to a New Page or Directory

Let’s say you re-do your entire website, renaming pages and directories. Visitors to the old pages will receive the 404 File Not Found error. You can solve this problem by redirecting calls to an old page to the new page. For example, if your old page was named oldpage.html and newpage.html has replaced that page, add this line to your .htaccess file:

Redirect permanent /oldpage.html http://www.mydomain.com/newpage.html

Of course, you want to replace mydomain.com with your actual domain name. Now, when the visitor types in http://www.mydomain.com/myoldpage.html, they will be automatically redirected to http://www.mydomain.com/mynewpage.html.

If you’ve renamed a directory, you can use one redirect line to affect all pages within the directory:

Redirect permanent /olddirectory http://www.mydomain.com/newdirectory/

Note that the old page or directory is specified using the system path relative to your www directory, while the absolute URL specifies the new page or directory.

Prevent Hot Linking and Bandwidth Leeching

What if another website owner is stealing your images and your bandwidth by linking directly to your image files from his/her website? You can prevent this by adding this to your .htaccess file:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?mydomain.com/.*$ [NC]
RewriteRule .(gif|jpg)$ - [F]

Replace mydomain.com with your actual domain name. With this code in place, your images will only display when the visitor is browsing http://mydomain.com. Images linked from other domains will appear as broken images.

If you’re feeling particularly nasty, you can even provide an alternative image to display on the hot linked pages – for example, an image that says “Stealing is Bad … visit http://mydomain.com to see the real picture that belongs here.” Use this code to accomplish that:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?mydomain.com/.*$ [NC]
RewriteRule .(gif|jpg)$ http://www.mydomain.com/dontsteal.gif [R,L]

This time, replace mydomain.com with your domain name and replace dontsteal.gif with the file name of the image you’ve created to discourage hotlinking.

Prevent Viewing of .htaccess or Other Files

To prevent visitors from seeing the contents of your .htaccess file, place the following code in the file:

<Files .htaccess>
order allow,deny
deny from all
</Files>

If you want to prevent visitors from seeing another file, just substitute that file’s name for .htaccess in the Files specification.