Link to home
Start Free TrialLog in
Avatar of bider2k
bider2k

asked on

Parse part of a path and redirect to another domain with that part

I have 2 domains: example1.com and example2.com.  

On example2.com there are a bunch of vanity urls like example2.com/first.middle.last for thousands of people.  

But, users keep going example1.com looking for the vanity urls, which don't exist there.  

I need to figure out how to redirect users who go to example1.com looking for those vanity urls to example2.com with the vanity url --- without setting up the thousands of redirects on example1.com.  

I don't have access to the server, so I'm thinking a javascript solution would work best.  Some JS on the error page of example1.com that would parse the URL for the /first.middle.last pattern and redirect them to example2.com/first.middle.last.  

Avatar of uk44
uk44

Something like this should work for you.

<html>
<script type="text/javascript">
var url = document.location.href.replace('location1','location2');
document.location.href=url;
</script>
</html>

It pulls the address of the page, performs a search/replace on it, and refreshes the browser to the new page. This should keep your vanity URI intact on the new domain.
<html>
<script type="text/javascript">
var url = document.location.href.replace('location1','location2');
document.location.href=url;
</script>
</html>

Open in new window

Avatar of bider2k

ASKER

Cool.  That will do the domain switch.  

But since I need to put this on the example1.com's error page, I need to just execute this script when someone gets the 404 error when looking for a url with that pattern: example1.com/first.middle.last.  The pattern would always have the 2 dots separating the first name, middle initial, and last name.  

I don't want the redirect to happen when an actual 404 is generated --- say if a user requests a url that is actually not found like example1.com/about/contacct.html.

Avatar of bider2k

ASKER

I just put this on my IIS localhost error page, and it doesn't seem to be taking the vanity part of the URL when it does the redirect.
Do you have a link where I could see it in action? This might help to diagnose why it's not working;
Avatar of bider2k

ASKER

Oops, I was wrong.  It is working.  I just need to figure out how to parse the url for the first.middle.last pattern.  Any ideas?
ASKER CERTIFIED SOLUTION
Avatar of uk44
uk44

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bider2k

ASKER

That is exactly it.  Thank you so much!