Optimizing Your Website Using htaccess

Search Engine Optimization September 10th, 2009

By

htaccess-seo

Optimizing Your Website Using htaccessHTACCESS?  Is this some sort of VIP area that only a select group of people get to go to at the nightclub?  Is it a secret underground group?  Well if you are a web developer or someone that optimizes web sites for a living, then you definitely know already.

.htaccess can help in a variety of ways to optimize a website for search engines and thereby helps to have better rankings.  This article is dedicated to those who want to learn more about what .htaccess is and also learn how it can help your website get found more easily.

What is .htaccess?

Hypertext Access, commonly shortened to .htaccess, is a configuration file which controls the directory it is placed in and all the subdirectories underneath it. It’s an incredibly useful feature which allows webmasters to control how many aspects of their website works. The .htaccess file is a very powerful configuration file and should be used wisely as it can affect everything and anything about your website. Some of the things you can do with your .htaccess file include:

  • redirecting site pages
  • change the extensions of pages
  • password protect directories
  • and much much more.

The file itself is just a small basic text file and can be edited. To learn more about .htaccess visit this site.

How to create .htaccess file?

Here’s how to create a text file suitable for an .htaccess file:

  1. Open up a text editor on your computer. We recommend that you use Notepad.
  2. Save the file  as .htaccess (including the initial dot).
  3. Make sure that your text editor did not append a .txt suffix to the file name.  If the file name does have a .txt suffix (i.e. '.htaccess.txt'), remove the suffix by renaming the file.
  4. Before entering code into your new file, make sure that 'Word Wrap' is turned off. This is important because most .htaccess commands need to be placed on one line only.
  5. You can now enter your code into the file.
  6. Upload the .htaccess file into your web space in ASCII mode. The .htaccess file will affect the folder that it is uploaded to and all its subfolders.

Features of .htaccess

There are many features and functionalities in .htaccess that can make a website more flexible, secured and SEO friendly. Mentioned here is only the most widely used features which can be implemented by anyone who has basic web knowledge.

1. Redirects

.htaccess uses redirects to look for any request for a specific page and if it finds that request, it forwards it to a new page you have specified. This is especially useful when you move your website to a new domain or changed the existing file names of your website.

Redirects are useful because if your pages are indexed by Google and you have changed the file / folder name or moved your pages filenames, search engines will use the .htaccess to help guide them to where the new pages are found. (In the industry, we call this a 301 redirect)

Simple Redirect

Here is the code for a simple redirect :-

Redirect /olddirectory/oldfile.html http://yoursite.com/newfile.html

Here when a request for old directory/oldfile.html is received, it is redirected to the new link.  In this case:

yoursite.com/newfile.html

301 Redirect

As mentioned earlier, a 301 redirect is the most efficient and 'Search Engine Friendly' method for web page redirection. It's not that hard to implement and it will preserve your search engine rankings for any page that you list in the .htaccess file. If you have to change file names or move pages around, this is the safest option. The code "301" is interpreted online as "moved permanently".

Redirect 301 /oldpage.html http://www.yoursite.com/newpage.html

Redirecting non www URL's to www URL

What if you want your domain to get displayed or redirect in to a standardized format? A domain can be displayed in all the following ways:

  • http://mywebsite.com or
  • www.mywebsite.com or
  • http://www.mywebsite.com.

The latter one is the standard and ensuring the others display in this format can be done with the following code:

Options +FollowSymLinks
RewriteEngine on

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

This code will make sure that the URL is standardized to display the 'www' and will be of much help in tracking the pages / visits.

Why would you want to do this?

Often if you do not make this change search engines can index each version and get confused into thinking that they are reading the same page 3 times over.  With this adjustment search engines are then pushed down the proper path and no same page will be treated as a different URL.

2. Handling Errors

htaccess also provides methods to handle errors when an invalid  / false request is received by the server.

ErrorDocument 404

The ErrorDocument 404 will give you a chance to handle requests for pages not found. This situation may happen if the link pointed to a domain is not correct or the user changes the URL string / query string (after the domain name).

In order to specify your own customized error documents, you simply need to add the following command, on one line, within your htaccess file:

ErrorDocument code /directory/filename.ext

or

ErrorDocument 404 /errors/notfound.html

This would cause any error code resulting in 404 to be forwarded to yoursite.com/errors/notfound.html

Here are the other important Error Handlers using .htaccess

When a bad request comes to the server use :-

ErrorDocument 400 /errors/badrequest.html 

When trying to access a page where authorization is required use:

ErrorDocument 401 /errors/authreqd.html 

When encountering an Internal server error use:

ErrorDocument 500 /errors/serverr.html 

3. Security

Preventing Directory Listing

Do you have a directory full of images or zip files that you do not want people to be able to browse through? Create a .htaccess file inside any folder to disable the directory listing and insert the following :

IndexIgnore *

The below code would return a list of all files not ending in .jpg or .gif, but would still list .txt, .html, etc.

IndexIgnore *.gif *.jpg 

If your server is setup to prevent directory listing, but you want to list the directories by default, you could simply throw this into a .htaccess file for the directory you want displayed:

Options + Indexes

Blocking users by IP

Tired of getting hacked and you can see a specific IP constantly in your log files?

You can deny access based upon IP address or an IP block.

order allow,deny
deny from 123.45.6.7
deny from 012.34.5.
allow from all

The above blocks access to the site from 123.45.6.7, and from any sub domain under the

IP block 012.34.5.

You can also allow or deny by domain name rather than IP address like deny from .spamsite.com works for www.spamsite.com or virtual.spamsite.com, etc.

Prevent Hotlinking

Also known as ‘bandwidth stealing’, hotlinking refers to linking directly to  images, .js files etc. of another server. The victim's server in this case is robbed of bandwidth as the violator enjoys showing content without having to pay for the delivery. For better performance of your server don’t forget to add this code snippet.

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

Be sure to replace "mydomain.com" with your own. The above code creates a failed request when hot linking of the specified file types occurs.

Password protection

You can also protect your website or particular folders password protected using .htaccess and .htpasswd. For more details here is a nice little post

4. SEO Friendly URL’s

It's common to need to redirect dynamic URL's with parameters to a single static file:

RewriteEngine On

RewriteRule ^news.php?id=(.*)$ /latestnews.htm [L,R=301]
In the above example, a request to a dynamic URL such as http://www.mysite.com/news.php?id=8932 will be redirected to http://www.mysite.com/latestnews.htm

The below code will redirect productinfo.php?prodID=12 to products/12/ or products/99/

RewriteEngine On

RewriteRule ^products/([0-9][0-9])/$ /productinfo.php?prodID=$1

This will match any URLs that start with ‘products/’, followed by any two digits, followed by a forward slash.

For example, this rule will match a URL like products/12/ or products/99/, and redirect it to the PHP page.

To comment a line in .htaccess just start the line with a hash (#).

Summary

Each and every step mentioned as part of this blog can improve the performance of a website drastically and will make sure your web site is more SEO friendly.  Hackers will also have a tough time if security measures are enabled using .htaccess. The benefits of implementing .htaccess is that it requires no programming knowledge and can save a great deal of time.

If you are a developer or code junkie like me then you will find this article very helpful.  If you read the top, glazed through all that code above and then read the summary this may be something that you send to your SEO optimization company or even your developer.  This article could give you (ahem) 'access' to a great deal of benefits!

Post By Jay (9 Posts)

Website: →

Connect

avatar
By

Post a Comment

Send me SEO tips!

5 Comments