Solved

redirect non www to www url using Javascript

Posted on 2011-02-24
18
1,375 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
Comment Utility
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
Comment Utility
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
Comment Utility
leakim971, can you please explain how your magic solution works?
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>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
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 38

Expert Comment

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

Author Comment

by:ken_rgr
Comment Utility
What is "htaccess? ".
Can you provide some example.

Thanks
0
 
LVL 82

Expert Comment

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

0
 

Author Comment

by:ken_rgr
Comment Utility
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
Comment Utility
0
 

Author Comment

by:ken_rgr
Comment Utility
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
Comment Utility
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
Comment Utility
Excellent solution
0
 
LVL 82

Expert Comment

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

Open in new window

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to dynamically set the form action using jQuery.
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)

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now