Solved

How do I match URL parts

Posted on 2010-11-23
15
281 Views
Last Modified: 2012-05-10
I am trying to add "www" to the front of my URL if the user didn't add it. The problem is that I have several possible entries that I don't want changed.

The addresses I don't want changed are as follows:
www.sitename.com
origin.sitename.com
ads.sitename.com
172.sitename.com
in.sitename.com
192.sitename.com

But, if the URL is "http://sitename.com" or "sitename.com", I need it to change to "http://www.sitename.com" or "www.sitename.com" respectively.

Here is my code:
<script type="text/javascript">
   var oldUrl = window.location.href;.
   var myRegExp = "www.|origin.|ads.|172.|in.|192.";
   var string1 = window.location.href;
   var matchPos1 = string1.search(myRegExp);
   if(matchPos1 != -1)
      // this is where I need to rewrite the URL
</script>

Another problem is that the "in" part is in some of the other parts of the URL.

Thanks in advance for the help.
0
Comment
Question by:akdev
  • 8
  • 5
  • 2
15 Comments
 
LVL 16

Expert Comment

by:jmatix
ID: 34200391

var oldUrl = window.location.href;

if (!oldUrl.match(/^(www\.|origin\.|ads\.|172\.|in\.|192\.)/))
   window.location.href = "www."+oldUrl;
0
 

Author Comment

by:akdev
ID: 34204966
Thanks jmatix,

This is really close, but the following is happening:

This is the address I am going to
http://sitename.com/item_detail.html

And the code is trying to put the "www." in front of the "http".

This is the error message I am getting.
Firefox doesn't know how to open this address, because the protocol (www.http) isn't associated with any program.

Any ideas?

Thanks.
0
 
LVL 16

Expert Comment

by:jmatix
ID: 34205523
Try this:

var oldUrl = window.location.href;

if (!oldUrl.match(/^(https?:\/\/)?(www\.|origin\.|ads\.|172\.|in\.|192\.)/))
      window.location.href = oldUrl.replace(/^(https?:\/\/)?/i, "$1www.");
0
 

Author Comment

by:akdev
ID: 34205820
imatix,

I put in the code. Can't get it to work. To test it, I am at address, http://172.16.1.12/item_detail.html. I changed the "172." in the match string to "173." to see if it changed my URL for me. It didn't.

Thanks.
0
 
LVL 8

Expert Comment

by:kivan24
ID: 34205876
Try this:
var oldUrl = window.location.href;
window.location.href = oldUrl.replace(/^(https*:\/\/)*((?!www|ads|origin|172|192|in).+)*$/img, "$1www.$2");

Open in new window

0
 

Author Comment

by:akdev
ID: 34205963
In IE8, I am getting an error that says it is expecting a ")" in reg ex. It looks like all the parenthesis are there.

The line it is complaining about is:
if(!oldUrl.match(/^(https?:\/\/)?(www\.|origin\.|ads\.|173123\.|in\.|192\.)/))
0
 

Author Comment

by:akdev
ID: 34206045
IE8 is also erroring with the message for the new line you gave:

window.location.href = oldUrl.replace(/^(https*:\/\/)*((?!www|ads|origin|172|192|in).+)*$/img, "$1www.$2");

With this line, I am thinking I don't need the "if" statement, correct?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 16

Expert Comment

by:jmatix
ID: 34206088
When I try http://173.16.1.12/item_detail.html, I get:

http://www.173.16.1.12/item_detail.html

That is the expected output. Right?
0
 
LVL 8

Expert Comment

by:kivan24
ID: 34206186
With this line, I am thinking I don't need the "if" statement, correct?
Yes correct! Just two lines i gave. Work fine in IE8.

Try this code, is he work correct?
<script type="text/javascript">
var mass = [ "www.sitename.com",
             "origin.experts-exchange.com",
             "ads.sdobe.com",
             "172.sitename.com",
             "in.sitename.com",
             "192.sitename.com",
             "http://sitename.com/",
             "https://sitename.com/",
             "sitename.com",
             "zzz.microsoft.com",
             "10.sitename.com"
];

var tempstr;
for(var i=0; i<mass.length; i++) {
   tempstr = mass[i].replace(/^(https*:\/\/)*((?!www|ads|origin|172|192|in).+)*$/img, "$1www.$2");
   alert(mass[i]+"---"+tempstr);
}
</script>

Open in new window

0
 

Author Comment

by:akdev
ID: 34207099
jmatix,

This is the code that I am using from your suggestion. It is giving me a js error on line 121, char 28 in IE8.

line 119: <script type="text/javascript">
line 120: var oldUrl = window.location.href;
line 121: if(!oldUrl.match(/^(https?://)?(www.|origin.|ads.|173123.|in.|192.)/))
line 122:   window.location.href = oldUrl.replace(/^(https*://)*((?!www|ads|origin|173|192|in).+)*$/img, "$1www.$2");
line 123: </script>

--------------------------------------------------------------------------------------

kivan24,

This is the code that I am using from your suggestion. It is giving me a js error on line 123, char 48 in IE8.

line 121: <script type="text/javascript">
line 122: var oldUrl = window.location.href;
line 123: window.location.href = oldUrl.replace(/^(http*://)*((?!www|ads|origin|173|192|in).+)*$/img, "$1www.$2");
line 124: </script>
0
 
LVL 16

Expert Comment

by:jmatix
ID: 34207206
You need to escape / and . with \ as I had given:

var oldUrl = window.location.href;

if (!oldUrl.match(/^(https?:\/\/)?(www\.|origin\.|ads\.|172\.|in\.|192\.)/))
      window.location.href = oldUrl.replace(/^(https?:\/\/)?/i, "$1www.");

0
 

Author Comment

by:akdev
ID: 34207309
Sorry imatix,

That was the view source I listed before. This is what is in the actual code:

<script type="text/javascript">
   var oldUrl = window.location.href;
   if (!oldUrl.match(/^(https?:\/\/)?(www\.|origin\.|ads\.|172\.|in\.|192\.)/))
      window.location.href = oldUrl.replace(/^(https?:\/\/)?/i, "$1www.");
</script>

There is still a js error saying "Expected ')' in regular expression".

Thanks.
0
 
LVL 16

Accepted Solution

by:
jmatix earned 500 total points
ID: 34207425
Umm. View source did not show the \ . That means \ s are getting eaten up at the server side. Is it ASP or PHP generated page? Try this:

var oldUrl = window.location.href;
var regex1 = new RegExp("^(https?://)?(www\.|origin\.|ads\.|173123\.|in\.|192\.)","i");
var regex2 = new RegExp("^(https*:\/\/)*((?!www|ads|origin|173|192|in).+)*$","img");
if(!oldUrl.match(regex1))
    window.location.href = oldUrl.replace(regex2, "$1www.$2");
0
 

Author Comment

by:akdev
ID: 34207482
Awesome imatix,

That worked. Thanks so much for your help and diligence.

0
 

Author Closing Comment

by:akdev
ID: 34207497
Stayed with my problem until solved. Nice job.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

With more and more people tethered to the internet, advertisers can reach you in new and creative ways. Push notifications on your smartphone, customized advertisements based on your previous searches, and email inboxes filled with promotional pitch…
Note: This is the second blog post in a series on email clearinghouses (https://www.xmatters.com/alert-management/blog-email-has-failed-us?utm_campaign=70138000000ydLoAAI&utm_source=exex&utm_medium=article&utm_content=blog-post).   Every month t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

929 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

9 Experts available now in Live!

Get 1:1 Help Now