Quantcast
Channel: Order of Business » Web Hosting
Viewing all articles
Browse latest Browse all 2

WWW Redirects with .htaccess

$
0
0

Definition of a www redirect

A www redirect is a rule on your web server that forwards all traffic from the non-www version of your domain to the www version, or vice versa.  For example, lets say your website is ‘www.example.com’ and someone types in ‘example.com’ into their browser.  The browser will send them to ‘example.com’.  When that request hits your web server, it will indicate to the browser that the correct web address is actually ‘www.example.com’ and will send the user there instead.  You can also setup redirects that work the other way and send users from ‘www.example.com’ to ‘example.com’.

The HTTP protocol used on the web has a numerical system for identifying the status of page requests.  The 301 status code indicates that the page or resource requested has ‘moved permanently’.  When implementing your www redirect it is important that you return a 301 status code so that search engines are clear which URL should be indexed.

Avoiding Duplicate Content with redirects

Every website has what is called the ‘site root’.  That is the directory on your web host that is publicly available when a user goes to your domain.  By default, this directory can be accessed by typing in the domain two different ways ‘www.example.com’ or ‘example.com’.  This means that for every page on your website, it can be accessed via two different URLs: the www version and the non-www version.  When search engines, such as Google, come along to crawl and index your site, they may come to the same page via the two different versions of the url.  When a search engine finds the same content at different URLs, it is called duplicate content.

Duplicate content is an issue because it puts your own site in competition with itself.  A page that might normally have great rankings in search now has another copy of itself in the search index.  When a user types in a keyword, how does the search engine know which page to display?  Is one copy better than the other?  To find the answer, search engines will look at off-page factors such as the number of incoming links.  If you have links pointing to just one of the pages, then that one will probably be selected.  If not, then that means you have users linking to two different URLs for the same content.  Imagine how much better you would rank in search if all those links were for the same page.  Allowing the same content to be accessed via multiple URLs dilutes your SEO efforts and devalues your content in the eyes of search engines.  The fix is to simply redirect everyone, search engines included, to the desired URL using 301 redirects.

Locating and working with your .htaccess file

As of February 2012, the Apache web server is most common, with just over 64% of the market share on the web.  If you aren’t using an Apache server, then what I am about to tell you won’t work.  If you are, then you will need to locate or create an .htaccess file at your site’s root directory.  The .htaccess file may, or may not, be visible when viewing your sites file structure through cPanel or your favorite FTP editor.  If you don’t see your .htaccess file, look around for an option that will allow you to see hidden files.  If you still don’t see it, then go ahead and create an .htaccess file.  If you have to create one, just upload an empty text file and rename it to ‘.htaccess’ ( NOT ‘.htaccess.txt’ ).  Once you have found or created your .htaccess file, then you just need to place your redirect rules within it.

If your .htaccess file already has stuff in it, make a copy and save it somewhere in case you have to revert back.  There is nothing worse than breaking your entire site because you messed up a key file and didn’t save a backup.

Implementing the www redirect

First, make sure that mod_rewrite is enabled.  Just make sure that this line appears somewhere in your .htaccess file above the rule that we are about to add:

RewriteEngine On

To redirect from non-www to the www version of your site, use the following rule:

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

To redirect from www to the non-www version of your site, use the following rule:

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

Make sure that you replace ‘example.com’ with your actual domain!

WWW Redirects with .htaccess is a post from Order of Business.


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images