[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

redirect non www to www url using Javascript

Posted on 2011-02-24
18
Medium Priority
?
1,887 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 3
  • +1
18 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 1000 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 1000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 39

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 39

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 39

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
This article discusses how to implement server side field validation and display customized error messages to the client.
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)
Suggested Courses

649 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