Using Htaccess

Here's some tips how to use .htaccess files properly. If you need password protected directories, check out this generator which will do that for you.

Custom error pages

Custom error pages are very useful. Instead of giving surfer blank page when he/she enter URL that is not there, you can setup .htaccess file so that surfer goes to page you want. Below are lines you need to add into .htaccess file to get things working. You can specify what ever .html page you want. Even different domain. Just modify those in red.

ErrorDocument 404 404.html
ErrorDocument 500 error.html

After adding these to .htaccess file, upload it to server and try by typing URL that you know is not in your server. If all is ok, you'll get custom error page you made.


Using shtml includes and stuff in HTML documents

By default only pages ending in the .shtml extension will parse server-side includes (SSI). You can override this with little modification of 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 via FTP access ) to your main www directory. Add the following lines to your .htaccess file:

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

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


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 individual uses to connect to the Internet, you can use .htaccess to block that individual from your web site.

<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 web site, 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

Visitors to the old pages that were removed get 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 old.html and that page has been replaced by new.html, add this line to your .htaccess file:

Redirect permanent /old.html http://www.domain.com/new.html

Replace domain.com with your actual domain name. When the visitor types in http://www.domain.com/old.html, they will be automatically redirected to http://www.domain.com/new.html.

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

Redirect permanent /olddir http://www.domain.com/newdir/

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


Prevent Hot Linking and Bandwidth Leeching

Hot linking is , sad to say but very usual in web so better be protected. Simple way is do it via htaccess file. You can prevent this by adding this to your .htaccess file:

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

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

You can also use image to show people hotlinking. ;)

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

This time, replace domain.com with your domain name, and replace nasty.gif with the file name of the image you've created to discourage hot linking. Drop something nasty here or better - advertise your own site!

Sometimes this won't work so you can try .htaccess file generator. Here's couple good ones you can use for free.

http://www.htaccesstools.com/hotlink-protection/


Porn-Hawk.com for Webmasters:
Submit freesite | See our rules | Our Recip links | SE Submit 'tool'


 

Last modified: 2005-05-25
-JK- wolfie