Solved

redirect non www to www url using Javascript

Posted on 2011-02-24
18
1,484 Views
Last Modified: 2012-05-19
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
0
Comment
Question by:ken_rgr
  • 7
  • 5
  • 3
  • +1
18 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 250 total points
ID: 34976351
You may use :
if( !/\.?www./g.test(location.host) ) location.href = location.href.replace("://","://www.")

Open in new window

0
 
LVL 16

Assisted Solution

by:sjklein42
sjklein42 earned 250 total points
ID: 34976363
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
 
LVL 16

Expert Comment

by:sjklein42
ID: 34976381
leakim971, can you please explain how your magic solution works?
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 82

Expert Comment

by:leakim971
ID: 34976459
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
 

Author Comment

by:ken_rgr
ID: 34976481
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
 

Author Comment

by:ken_rgr
ID: 34976508
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
 
LVL 82

Expert Comment

by:leakim971
ID: 34976523
>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
 
LVL 16

Expert Comment

by:sjklein42
ID: 34976561
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
 

Author Comment

by:ken_rgr
ID: 34976609
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
 
LVL 38

Expert Comment

by:Aaron Tomosky
ID: 34976708
Search engines don't do JavaScript. Why aren't you doing this in .htaccess?
0
 

Author Comment

by:ken_rgr
ID: 34976784
What is "htaccess? ".
Can you provide some example.

Thanks
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34976829
>I am trying to reach you for the last 2 days.
How ?

0
 

Author Comment

by:ken_rgr
ID: 34976867
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
 
LVL 38

Expert Comment

by:Aaron Tomosky
ID: 34976871
0
 

Author Comment

by:ken_rgr
ID: 34980010
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
 
LVL 38

Expert Comment

by:Aaron Tomosky
ID: 34980179
Sorry, for iis it's easy. Just make your main site nonwww, then make a www site that redirects to it.
0
 

Author Closing Comment

by:ken_rgr
ID: 34981303
Excellent solution
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34985238
updated :
if( !/\.www\.|^www\./gi.test(location.hostname) ) location.href = location.href.replace("://","://www.")

Open in new window

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Jquery 10 20
Optimize simple Javascript code to use no repetitions 12 32
alert(innerHTML); 8 19
how can i get 4 nd 5 ? 3 13
Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question