Using the .htaccess File

A breif tutorial on how to use the .htaccess file to set up custom (404) error pages, redirect domain names, and protect your images from "hotlinking".

 

What is the .htaccess file?


Setting up custom error pages


Using RewriteRule

 
Protecting your images

 

 

What is the .htaccess file?

 

 

The .htaccess file is a simple text file that can be used to configure server side aspects of your account. It is important to note that the file must be named:

 

.htaccess

 

 If it is named anything else (eg: .htaccess.txt) it will not work. The .htaccess file controls all of the directories beneath it in the directory structure. If you place your .htaccess file in your main directory, it will influence all of the directories in your account.

 

 

 

Creating Custom Error Pages

 

 

You may create your own custom error pages (including File not found) page for your site. You can specify error pages for each type of error - the most common being the 404 File Not Found error. To specify a page to use for all 404 errors put these lines in the .htaccess file:

 

 

# 404 handles File not found errors
ErrorDocument 404 http://www.example.com/error.html

 

If you want to add pages for other errors, just add the new lines directly below the 404 line:

 

# 403 handles Forbidden errors
ErrorDocument 403 http://www.example.com/error2.html
# 500 Handles internal server errors
ErrorDocument 500 http://www.example.com/error3.html

 

 

 

Using RewriteRule

 

 

There are lots of neat things that you can do with your .htaccess file and mod_rewrite. One handy one is if you have multiple domains names on your account (lets say example1.com and example2.net), and you want the secondary (example2.net) to redirect to your main domain name (example1.com). Just place these three lines into your .htaccess, and the server will take care of the rest:

 

RewriteEngine on
RewriteCond %{SERVER_NAME} ^(www.)?example2.net
RewriteRule ^(.*)$ http://www.example1.com/$1 [R=permanent,L] 

 

What this rewrite rule does is it simple. If a person uses the URL 'www.example2.net' to access your page then they will redirect to 'www.example1.com'. This happens automatically in the background before the page even gets a chance to load.

 

 

 

Protecting your images

 

 

If you have a large amount of images on your site someone might decide to link to them and pass it off as their content. Adding this code to your .htaccess will stop them cold in their tracks. On top of that it will redirect the surfer who clicks on their download link to whatever page you wish. The code to put in your .htaccess:

 

RewriteEngine on 
RewriteCond %{HTTP_REFERER} !^$ 
RewriteCond %{HTTP_REFERER} !^http://(www.)?example.com [NC] 
RewriteCond %{HTTP_REFERER} !^http://YOURIPADDRESS 
RewriteRule .(gif|jpg|jpeg|bmp|png|mpg|mpeg|avi|wmv|mov|asf)$ - [F] 

 

 

The first "RewriteCond" line specifies that if there is no referrer, that's ok. The second "RewriteCond" specifies that if there is a referrer, it needs to be your domain name. The third line is an additional catch to allow you to use your IP address as well. The "RewriteRule" line denies anyone who breaks the above rules while trying to get a file that ends with one of the listed extensions.

 

 

Please let us know if you have any questions or need any help!

Was this answer helpful?

 Print this Article

Also Read

Server Side Includes (SSI)

Covers what is required to use Server Side Includes in your account, some sample SSI code, and...

PHP

Details on our PHP installations, as well as links to PHP resources.   What is PHP? PHP File...

Using Perl

Details for running Perl scripts on our shared servers.   Installing Scripts CGI Folder...