Jul 19 2010

Redirect based on the URL using JavaScript

 

There are many methods out there to do such a task, it seem many PHP solutions for this and also some JavaScript functions to do this, but they all seem overkill in terms of what’s required to be changes for each redirect.

here is a script with minimal changes required to get it working, this solution is based on the ever popular JavaScript.

A perfect example of using a script like this is if you are trying to target a particular market, for example. I am based in Australia, and I have a .com.au domain, and a .com domain. I might have a different offering or market to what I offer in Australia, so with that said I have 1 website but with multiple alias’s and pages for different markets. I need to somehow separate these with the URL, as .com.au tells me AU, .com tells me global.

So how do you use the below script easy:

Change:

“MyFirstDomaiName.com.au/MyFolder"

to your domain (without the www) and folder location

"http://www.MyFirstDomaiName.com.au/RedirectToFolder1/default.aspx"

Change this to be your location you wish to redirect to

http://www.MySecondDomaiName.com/RedirectToFolder2/default.aspx

this is the second URL you want it to redirect too.

<html>

<head>

<script language="javascript">

 

 

    var href = document.location.href;

 

    if (href.indexOf("MyFirstDomaiName.com.au/MyFolder") > 0) 

        document.location.href ="http://www.MyFirstDomaiName.com.au/RedirectToFolder1/default.aspx"

    else

        document.location.href ="http://www.MySecondDomaiName.com/RedirectToFolder2/default.aspx"

 

 

</script>

</head>

</html>