thoughts on web design, development and seo bloggers

.htaccess and its use

June 17th, 2007 by Blogge SEO         
1 Star2 Stars3 Stars4 Stars5 Stars (6 votes, average: 4.5 out of 5)
Loading ... Loading ...

 

The Apache Web server provides a feature called .htaccess file, which provides commands to control a Web site. This file is simply a text file containing Apache directives. Those directives apply to the documents in the directory where the file is located, and to all subdirectories below it as well. Other .htaccess files in subdirectories may change or
nullify the effects of those in parent directories.

You have to be careful when editing .htaccess files, as a small mistake can make your Web site stop working. You should immediately test the site to be sure it works.

You can use any text editor to create or make changes to .htaccess files.

Be sure .htaccess file is uploaded in ASCII mode, not BINARY, or it won’t work.

Here are the most notable and useful .htaccess examples…

Custom error pages

The most common errors are 404 (Not Found) and 500 (Internal Server Error).
Design your custom Web pages for these errors (you aren’t limited to these errors,
you can create an error page for each and every error).

Add the following commands to your .htaccess file…

ErrorDocument 404 /404.html
ErrorDocument 500 /500.html

You can name the pages anything you want, and you can place them anywhere you want within your site. The initial slash in the directory location represents the root directory of your site.

Enabling SSI

If you want to use SSI, but can’t do so with your current Web host, you can change that
with .htaccess file. The following lines tell the server that any file named .shtml should
be parsed for server side commands…

AddType text/html .shtml
AddHandler server-parsed .shtml
Options Indexes FollowSymLinks Includes

If you don’t care about the performance hit of having all .html files parsed for SSI,
change the second line to…

AddHandler server-parsed .shtml .html

If you’re going to keep SSI pages with the extension of .shtml, and you want to use SSI
on your index pages, you need to add the following line to your .htaccess file…

DirectoryIndex index.shtml index.html

This allows a page named index.shtml to be your default page, and if that isn’t found,
index.html is loaded.

Redirects

You can use .htaccess file to redirect any request for a specific page to a new page…

Redirect /OldDir/old.html http://site.com/NewDir/new.html

Server-side redirects are very useful for shortening affiliate links. Your visitors won’t be turned off by long links that are obviously affiliate links.

or example, to create a redirect at the URL:

http://www.domain.com/link
to point to the URL:
http://www.DomainMerchant.com/affil.cgi?12345
put this line in your .htaccess file…

Redirect /link http://www.DomainMerchant.com/affil.cgi?12345

Protecting your bandwidth

“Bandwidth stealing,” also known as “hot linking,” is linking directly to non-html objects
on another server, such as images, electronic books etc. The most common practice of hot linking pertains to another site’s images.

To disallow hot linking on your server, create the following .htaccess file and upload it to the folder that contains the images you wish to protect…

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

Replace “YourSite.com” with your own.  The above code causes a broken image to be displayed whenit’s hot linked. If you’d like to display an alternate image in place of the hot linked one,replace the last line with…

RewriteRule \.(gif|jpg)$ http://www.YourSite.com/stop.gif [R,L]

Replace “YourSite.com” and stop.gif with your real names.

Preventing directory listing

IndexIgnore *

The * matches all files. If, for example, you want to prevent only listing of images, use…

IndexIgnore *.gif *.jpg

Redirecting YourSite.com to www.YourSite.com

If search engines find both www and non-www links from other sites to your site, they may treat
http://YourSite.com and http://www.YourSite.com as two different websites with the same content. This means that your site can be penalized for duplicate content.

Many experts recommend to set up a 301 redirect (permanent redirect) from YourSite.com to www.YourSite.com…

RewriteEngine On
RewriteCond %undefined ^YourSite\.com [nc]
RewriteRule (.*) http://www.YourSite.com/$1 [R=301,L]

Replace “YourSite.com” with your real domain name.

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

 

Posted in Webmaster | Check Comments | Share This
Tell a Friend Tell A Friend

One Response

  1. Seo Blogger Says:

    Article is helpful

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.