Click here to Skip to main content
11,489,693 members (73,267 online)
Click here to Skip to main content

Domain redirect using JavaScript

, 29 Oct 2001 68.7K 14
Rate this:
Please Sign up or sign in to vote.
How to Redirect a Domain to a file or directory using JavaScript


  • Use this script inside the <HEAD> </HEAD> tags of the index.html / index.htm (Apache) or default.htm / default.asp (IIS/PWS) file to redirect the Domains to their respective web page.
  • Initialize the dnsX variables with the Domains you wish to redirect and change their respective location.href
  • Last but not least, in the <BODY> tag call the JavaScript function using the "OnLoad" event: <BODY onLoad="gotoPage()">

Domain redirect using JavaScript

<script language="JavaScript">
function gotoPage()
    var dns1 = ""
    var dns2 = ""
    var dns3 = ""
    var dns4 = ""
    var dns5 = ""
    var dns6 = ""
    var urlloc = location.href
    var loc = urlloc.toLowerCase()

    if (loc.indexOf(dns1) != -1)

    if (loc.indexOf(dns2) != -1)

    if (loc.indexOf(dns3) != -1)
    if (loc.indexOf(dns4) != -1)

    if (loc.indexOf(dns5) != -1)

    if (loc.indexOf(dns6) != -1)


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Marco Ascensão
Web Developer
Portugal Portugal
No Biography provided

Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 30 Oct 2001
Article Copyright 2001 by Marco Ascensão
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid