Basic htaccess and You

Htaccess is a very useful tool that can help you direct traffic on your sites as well as maximize your site’s error pages and even protect you from hotlinkers. This article will discuss some of the more common ways to use htaccess.

The first thing I want to discuss is directing traffic from your error pages. The most common error page is the 404. Most people that get an error on your site will get this error. Normally the person would just see a blank page and nothing, but with htaccess you can send them to a custom page that says whatever you want on it. Doing this is very simple. First you will want to create the custom error page in html. It can have whatever you want on it. If you just want a simple page that says, “sorry you reached an invalid page” cool. If you want some ads or other sponsor stuff on it that is cool too. A mix of both also works well. Once you have your custom error page created call it error.html and upload it to your server. Now you will want to create your htaccess file. To do this simply open up notepad and you can type in this code:

ErrorDocument 404 /location on your server for error.html

So it might look like:

ErrorDocument 404 /404/error.html

You can do this for all the different types of error codes people may find on your site. The most common are:

ErrorDocument 300
ErrorDocument 301
ErrorDocument 302
ErrorDocument 303
ErrorDocument 400
ErrorDocument 401
ErrorDocument 403
ErrorDocument 404
ErrorDocument 500

You can make different pages for all of these errors if you want or you can just redirect them all to the same error page.

Another good use for htaccess is to protect your images from hotlinkers. You can do this by adding this code into the htaccess file:


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

This will protect any gif or jpg files in that directory from being hot linked. Many servers require you to upload a separate htaccess file for each directory with images. If you would like you can opt to serve an image to the hotlinker instead of the image they are looking for by using this code:

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

In addition to changing the domain name to fit yours change whatever.gif to be the image you want to serve if someone tries to hotlink you. If they do they will see that image instead of the image they wanted.

You can also use htaccess to redirect users from old or no longer existent pages to new pages on your site. The best way to do this is by using a 301 redirect. This does two things for you. First it allows you to redirect people from the old page to the new and it tells the search engines that the change is permanent so they will transfer any search engine weight that you might have for that old page to the new page. To do this add this line to you htaccess:

redirect 301 /old/old.htm http://www.domain.com/new.htm

Obviously you want to change that to reflect the location of you old and new pages on your server. If you don’t put the redirect 301 in front of the command the server will still redirect the page, but the search engines will see it as a temporary forward. This is something you might do if you need to take a page offline for a few minutes or hours to fix something on it. If you permanently keep it redirecting without the 301 you can lose any search engine strength you may have had.

The last thing I’m going to mention is the www rewrite command. This command makes sure that all visitors that type your site name into their browser will get to your site even if they don’t put the www in front of the domain name. Some servers do this automatically for you. If yours doesn’t you can do this with htaccess by putting this code in the file:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule (.*)$ http://www.domain.com/$1 [L,R=301]

With this code you will be sure people will find your site when they type your domain into their browser whether they use the www or not.


You can use any and all of these commands in a single htaccess file to give your site maximum protection and coverage.

Htaccess can be used for a lot of things including password protecting areas of your site and blocking users by URL or IP. Those are some more advanced things I will get into in another article. These are the most common and basic htaccess uses that will make directing and controlling traffic in your site a little easier.

Reader Comments: (2 posts)

Taron says:
You Sir/Madam are the enemy of confusion eevwryhere!
October 28th, 2011
at 8:39am EST
Rating:
Rating StarRating Star
ffff says:
assads
July 20th, 2010
at 11:17pm EST
Rating:
Rating StarRating StarRating StarRating StarRating Star

 

Post Your Comments

WARNING: Any comments you post are solely your responsibility. Webmastervault.com accepts no responsibility or liability whatsoever in connection with or arising from such content. Defamatory, derogatory, or other comments that we feel should be removed will, at our own discretion and ours alone.