.htaccess Tutorial
Home > Build
> Backend
by Jeremy Luebke
Many people know about the .htaccess script that can
be uploaded to Apache server but few know of it's full potential.
The most common use is for redirecting the default 404 error page
to a custom version but this versatile script is capable of so much
more. In this tutorial we will be covering how to use the .htaccess
script to:
- Create custom error pages.
- Enable .htm, .html pages to be able to handle Server Side Includes
(as opposed to just .shtm, and .shtml)
- Protect your bandwidth and content by only allowing downloads
to be made from certain domains/urls.
Create Custom Error Pages
We'll start with the most common use for this file.
This is pretty simple. The first step you want to take is to create
and upload the custom error pages to your server. The most common
error pages are 404, 401, and 500 but you can create a page for every
error under the sun.
After you have uploaded those html pages we need to
make the .htacces file. Open up note pad (or it's equivalent). The
contents of the file should look like this:
ErrorDocument 404 http://www.domain.com/404.html
ErrorDocument 500 http://www.domain.com/500.html
ErrorDocument 500 http://www.domain.com/401.html
Replace the urls with the ones you just uploaded then
save it as .htaccess. If windows won't let you name it .htaccess due
to there being no name in front of the period just name it htaccess
and then when you go to upload it through ftp use that program to
rename it to the proper .htaccess. When you do upload the file just
make sure to use ASCII mode and to put it in the public_ html directory.
Once you've done this, any errors will redirect people to your custom
pages.
Enable .htm, .html pages to be able to handle SSI
Most hosts will tell you that in order to be able to
use SSI on a webpage you will have to use .shtm, and .shtml extensions.
With this file it is possible to use the .htm, and .html extensions
for the same purpose.
You are basically going to follow the same steps that
we used to create custom error pages. Open up a text editor and insert
the following:
AddType text/html .shtml .shtm .htm .html
AddHandler server-parsed .shtml .shtm .htm .html
Then save as .htaccess and upload in ASCII mode to your
public_html directory.
Protect your bandwidth and content.
There are certain individuals out there that like to
steal content and bandwidth from other sites. If you have a large
amount of downloads on your site omeone might decide to link to them
and pass it off as their content. Adding this little file to your
site will stop them cold in their tracks. On top of that it will redirect
the surfer who clicks on their download link to whatever html page
you wish.
Follow the same steps in the last two examples and put
this text in the file:
AuthUserFile /dev/null
AuthGroupFile /dev/null
RewriteEngine On
RewriteCond %{HTTP_REFERER} !>http://www.coolresources.com
[NC]
RewriteCond %{HTTP_REFERER} !>http://coolresources.com [NC]
RewriteCond %{HTTP_REFERER} !>http://www.webmaster.coolresources.com
[NC]
RewriteCond %{HTTP_REFERER} !>http://webmaster.coolresources.com.com
[NC]
RewriteCond %{HTTP_REFERER} !>http://208.56.6.68 [NC]
RewriteRule /* http://www.iboost.com/index.html
[R,L]
"RewriteCond %{HTTP_REFERER} !>http://www.coolresources.com
[NC]" is the url or url's that the downloads will be allowed
from.
"RewriteRule /* http://www.iboost.com/index.html
[R,L]" is the page that the surfer will be redirected
to if they do try to download from an unauthorized url.
This file needs to be uploaded in ASCII mode to the
directory that contains the downloads that you wish to protect.