Website Administrator

    delivering administrator solutions for your website...


Date added: 6 July 2010
Last Mod: 28 Jun 2011
2010 Steve Campisi

Website Administrator Know How Articles

How to force the www prefix to your website location (Javascript)

Sometimes, the website administrator may wish to have his website use the "www" location prefix. The reasons for this may be many but it is most handy (especially if you use website statistics) to use only one of these internet paths to your website. Using Google's webmaster tools can help you sort this out for hits coming into your site through Google, but this will not catch all the hits coming into your webpages. If you monitor your webpage hits, you may have noticed that your website statistics record a hit to mywebsite.domain/index.html and another hit to www.mywebsite.domain/index.html as a hit on two different pages, even though the same page is displayed to the website visitor.

The best way for the website administrator to resolve this is to use apache's rewrite module. But what if you don't have access to the apache config file or your host has disabled your .htaccess file function? Well, you can always use javascript. And javascript is not a recommended seo technique because some use this method to hijack a web page to direct the visitor to what ever website you want. And as some do not use this method with good intentions, it's use has become somewhat frowned upon. However, it will resolve your dilemma should have you have no other recourse to force the www prefix to your website or if you don't care about bending for seo techniques.

The JavaScript Code to force the "www." prefix.

How we can redirect the hit to the www location is by using this copy and paste code given below, our MyDomain.com will now become www.mydomain.com and of course this only works if the user has javascript and although not 100%, this does cover the majority of website visitors. Also, make sure you place this between the <head> and </head> tags. (If your page already has these "head" tags, then don't copy the ones in this code.)

<script type="text/javascript">
<!-- you already know what this does // and this next code uses javascript to automatically go to a www domain
var hostname = location.hostname // grab browser's current domain hostname
var tempstring = new String(hostname) // make sure this is a string so it is easier to parse
var checkhostname = tempstring.toLowerCase() // and its also easier if we lower the case too please
if (checkhostname.indexOf("www") !=0 ) // now check that www is the very first part of our browser location string
{location.hostname = "www.mydomain.com"} // else give the browser this location instead and we are done and out -->
<meta http-equiv="Content-Script-Type" content="text/javascript" />

and that is all there is to this little lesson.

You can use this on as many of your web pages as you want, but really, you should add the full and correct code in your pages like this: <a href="http://www.MySite.com/linkedpage.html">link</a> for all of your internal website page links. This way, you should only need to add this javascript code to your index page and perhaps any other main landing page(s) that you have.

Want to test it?

I purposely have coded this as part of the html code of this page in case you wanted to test it. Backspace the http://www. part of the address in your browser's location bar above and then press the enter key and you will see that this page is very, very insistent you use the www address only, no matter how many times you try to get rid of it...

You can also view the page source of this page to see this code. (Of course javacript needs to be enabled for this code to work.)


You can also tell google to index your preferred website url by using the link rel canonical tag.


| downloads | home | articles index |