Home » Code Samples, SEO, Tutorials

.htaccess and SEO

21 August 2008 | 9,683 views | No Comment

Inbound Links

A .htaccess file can be an SEO’s vital friend. When visitors hit your site with either www.yourdomain.com or yourdomain.com, it splits your inbound links into two separate pages. This can have effects on your Google Page Rank. By correctly creating and implementing a .htaccess file at your server root, you can improve your website ranking.

To change the url in the browser before the page loads you can use two different methods.

Here, the url will always contain the www. before the domain name:

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

Here, you can take the www. away:

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

By deciding which of these ways of keeping your inbound links will work best, you can improve your search engine results.

301 Redirects

Redirect 301 / http://www.yournewdomain.com/

The above code is to be used when pointing multiple domain names to one domain (the main site) . This is often done by businesses wishing to protect their trademark by purchasing similar names and various top level domain names (TLD) for their company. This is also good for companies wishing to capture type in traffic of users who type keywords straight into the toolbars or their url area of the browser.

By using both of these methods, you can keep a tight seal around all of your inbound links, improve your SEO and Google Page Rank, and keep the files on your server more organized.

Keep Directory Index File Name Out of Your URL

Sometimes it just looks nicer to not have a url such as http://www.derekentringer.com/index.php. All we want is http://www.derekentringer.com.
Using your .htaccess you can eliminate it easily. You can also specify any directories that you might not want it to parse out the index file.

# Main Index exclude
DirectoryIndex index.php
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteCond %{REQUEST_URI} !^/DirectoryYouDoNotWantChangedHere
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

Now, there will never be an index file listed in the url, except for the directory “DirectoryYouDoNotWantChangedHere”.

Error Document Redirects

Instead of using a Control Panel to process redirects, you can use your .htaccess file. These links can either be redirected to your home page, or you can setup an error document.

# Handle Errors
ErrorDocument 100 http://www.derekentringer.com
ErrorDocument 101 http://www.derekentringer.com
ErrorDocument 102 http://www.derekentringer.com
# 2xx
ErrorDocument 200 http://www.derekentringer.com
ErrorDocument 201 http://www.derekentringer.com
ErrorDocument 202 http://www.derekentringer.com
ErrorDocument 203 http://www.derekentringer.com
ErrorDocument 204 http://www.derekentringer.com
ErrorDocument 205 http://www.derekentringer.com
ErrorDocument 206 http://www.derekentringer.com
ErrorDocument 207 http://www.derekentringer.com
# 4xx
ErrorDocument 400 http://www.derekentringer.com
ErrorDocument 401 http://www.derekentringer.com
ErrorDocument 402 http://www.derekentringer.com
ErrorDocument 403 http://www.derekentringer.com
ErrorDocument 404 http://www.derekentringer.com
ErrorDocument 405 http://www.derekentringer.com
ErrorDocument 406 http://www.derekentringer.com
ErrorDocument 407 http://www.derekentringer.com
ErrorDocument 408 http://www.derekentringer.com
ErrorDocument 409 http://www.derekentringer.com
ErrorDocument 410 http://www.derekentringer.com
ErrorDocument 411 http://www.derekentringer.com
ErrorDocument 412 http://www.derekentringer.com
ErrorDocument 413 http://www.derekentringer.com
ErrorDocument 414 http://www.derekentringer.com
ErrorDocument 415 http://www.derekentringer.com
ErrorDocument 416 http://www.derekentringer.com
ErrorDocument 417 http://www.derekentringer.com
ErrorDocument 418 http://www.derekentringer.com
ErrorDocument 419 http://www.derekentringer.com
ErrorDocument 420 http://www.derekentringer.com
ErrorDocument 421 http://www.derekentringer.com
ErrorDocument 422 http://www.derekentringer.com
ErrorDocument 423 http://www.derekentringer.com
ErrorDocument 424 http://www.derekentringer.com
ErrorDocument 425 http://www.derekentringer.com
ErrorDocument 426 http://www.derekentringer.com
# 5xx
ErrorDocument 500 http://www.derekentringer.com
ErrorDocument 501 http://www.derekentringer.com
ErrorDocument 502 http://www.derekentringer.com
ErrorDocument 503 http://www.derekentringer.com
ErrorDocument 504 http://www.derekentringer.com
ErrorDocument 505 http://www.derekentringer.com
ErrorDocument 506 http://www.derekentringer.com
ErrorDocument 507 http://www.derekentringer.com
ErrorDocument 508 http://www.derekentringer.com
ErrorDocument 509 http://www.derekentringer.com
ErrorDocument 510 http://www.derekentringer.com

Alternate to ErrorDocument

Instead of listing out all of the error types, you can also use the much shorter version:

### ALTERNATIVE TO USING ERRORDOCUMENT ###
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /error.php [L]

This will push all of your errors to a single page.

That’s really just the beginning of what the .htaccess file is capable of, as it has an extensive set of capabilities.

Read more about them here.

Download a sample .htaccess file