Redirecting your visitors to the preferred form of your URL using JavaScript
Do you have multiple entrances to your site and looking for a client-side solution that can redirect your visitors to the preferred entrance?
For example, you may have multiple domain names pointing to the same site --
domain_a.com,
domain_b.com and
domain_c.com. Although you want to keep all of your domains either because you want to reserve those names, because your marketing department convinced you that having multiple names pointing to the same site will attract more visitors or some other reason, you may prefer that the visitors end up at the same domain. You may want to redirect visitors from
domain_b.com and
domain_c.com to
domain_a.com for example.
Following is a client-side solution to redirecting visitors to your preferred domain. Simply copy and paste it on to your home page just below the
tag.
<script type="text/javascript" language="JavaScript">
<!--
if( -1 == location.href.
toLowerCase().
indexOf('domain_a.com') )
{
location.href = 'http://domain_a.com';
}
// -->
</script>
Listing #1 : JavaScript code.
Above code will check if
domain_a.com is a part of the current URL. If not, it will automatically redirect the visitor to
http://domain_a.com. One of the advantages is that this code will work even if you have a single version of your home page that has multiple domain names pointing to it.
Of course you can modify the same code to redirect to a specific page URL as well.
| NOTE: |
Be sure to use only the required portion of your URL and to enter it in lowercase inside the function "indexOf()". If your complete URL is http://www.mycompany.com/mypage.html, but you can get to the same page using just mycompany.com/mypage.html, use the latter version of the URL. On the other hand, use the full URL for location.href.
|
The following code will redirect visitors to
http://www.mycompany.com/mypage.html only if
mycompany.com/mypage.html is not a part of the URL they typed:
<script type="text/javascript" language="JavaScript">
<!--
if( -1 == location.href.
toLowerCase().
indexOf('mycompany.com/mypage.html') )
{
location.href =
'http://www.mycompany.com/mypage.html';
}
// -->
</script>
Listing #2 : JavaScript code.
If you run your own web server, have unique IP addresses for your multiple domains or your web space provider is able to redirect visitors otherwise, you maybe able to implement a server-side solution to redirect visitors instead.
Applicable Keywords : HTML, Internet Explorer, Explorer 3.x, Explorer 4.x, Explorer 5.x, Internet, JavaScript, JavaScript 1.0, JavaScript 1.1, JavaScript 1.2, Netscape Navigator, Navigator 2.x, Navigator 3.x, Communicator 4.x, Netscape 6.x, World Wide Web