redirect non www to www url using Javascript

Hi
In my website I  need to redirect all the  non www  urls to www urls.
Means If a url doesn't contains www then I need to dynamically add www infront of the
url,
This is very important as search engines treat these urls differently.
I need to do this using Client side Javascript.
Any suggestion how do it.

Thanks
ken_rgrAsked:
Who is Participating?
 
leakim971PluritechnicianCommented:
You may use :
if( !/\.?www./g.test(location.host) ) location.href = location.href.replace("://","://www.")

Open in new window

0
 
sjklein42Commented:
Put this near the top of your web page.  It does an automatic redirect with "www" added.

<script type="text/javascript">
if ( document.domain.substring(0,4) != 'www.' ) {
    window.location = document.URL.replace("//","//www."); 
}
</script>

Open in new window

0
 
sjklein42Commented:
leakim971, can you please explain how your magic solution works?
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
leakim971PluritechnicianCommented:
It use :
http://www.w3schools.com/jsref/prop_loc_hostname.asp
http://www.w3schools.com/jsref/jsref_regexp_test.asp
http://www.w3schools.com/jsref/jsref_regexp_zeroone.asp

we check the location.hostname to see if it contain .www. or start by www.

corrected :


if( !/\.www\.|\/www\./g.test(location.hostname) ) location.href = location.href.replace("://","://www.")

Open in new window

0
 
ken_rgrAuthor Commented:
Hi Guys

Thank You very much. Both of your solutions really worked well on my local system.
But I have to test it tommorrow on the Webserver.
After testing it on the webserver I will Accept Both of your solutions.

Thanks for your time.
0
 
ken_rgrAuthor Commented:
Hi Guys

One question.
Is it case sensitive. What if 'www' in Upper case.
Does it work??
Unfortunately I couldn't properly on my local system.

Thanks
0
 
leakim971PluritechnicianCommented:
>if( !/\.www\.|\/www\./g.test(location.hostname) ) location.href = location.href.replace("://","://www.")

just add << i >> after the << g >> in the regexp :


if( !/\.www\.|\/www\./gi.test(location.hostname) ) location.href = location.href.replace("://","://www.")

Open in new window

0
 
sjklein42Commented:
To make my version case insensitive:

<script type="text/javascript">
if ( document.domain.substring(0,4).toLowerCase() != 'www.' ) {
    window.location = document.URL.replace("//","//www.");
}
</script>
0
 
ken_rgrAuthor Commented:
Hi leakim971

I am trying to reach you for the last 2 days.
I have another question that is pending which is very urgent for me.
I know you sure can fix the issue

The question is "Image click not working in IE". Its still open in the other thread.

I really appreciate if you look at the question.

Thanks for your time
0
 
Aaron TomoskySD-WAN SimplifiedCommented:
Search engines don't do JavaScript. Why aren't you doing this in .htaccess?
0
 
ken_rgrAuthor Commented:
What is "htaccess? ".
Can you provide some example.

Thanks
0
 
leakim971PluritechnicianCommented:
>I am trying to reach you for the last 2 days.
How ?

0
 
ken_rgrAuthor Commented:
I know you are one of the Best Genius available in this Group.
Today I called(chat) the customer service to find out may be you can
look at my question. They said that all the experts are volunteers,
as and when they have time they will answer your question.
Unfortunately many people looked at the question and couldn't figured out
what the problem is.
I request you to look at the question if you have time, I am sure you can fix the issue.

Thanks for your time.
0
 
ken_rgrAuthor Commented:
Hi aarontomosky

I checked the link given by you.
But this .htaaccess file is for Apache server.
Our site is running in IIS.
We must do this redirect at the client side.
So do you think your solution will work?

Thanks for your time
0
 
Aaron TomoskySD-WAN SimplifiedCommented:
Sorry, for iis it's easy. Just make your main site nonwww, then make a www site that redirects to it.
0
 
ken_rgrAuthor Commented:
Excellent solution
0
 
leakim971PluritechnicianCommented:
updated :
if( !/\.www\.|^www\./gi.test(location.hostname) ) location.href = location.href.replace("://","://www.")

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.