DOMAIN

ID #1032

Redirect

Redirect Scripts to point a second domain to a sub-directory or sub-web of a site

Normally, domains can only be setup so that they point to the root directory of your web site. However, using ASP code, you can detect which URL a person is browsing to your site with, and redirect them to the appropriate place on your site. Here is some sample ASP code that will do the job. You will want to put this code at the very top of an .ASP page (generally, the default page of the primary domain) before any HTTP tags:

<%
sname = Request.ServerVariables("SERVER_NAME")
sname = ucase(sname)
if InStr(sname,"DOMAIN1") <> 0 then
response.redirect "subdirectory to go to"
elseif InStr(sname,"DOMAIN2") <> 0 then
response.redirect "2nd subdirectory to go to"
end if
%>


Make sure that the domains in the InStr commands are capital letters.


For example, suppose you have two domains, apple.com and banana.com, pointing to the same web site. On this site there is an "appledir" subdirectory (or sub-web) and a "bananadir" subdirectory. Then the code will look like this:

<%
sname = Request.ServerVariables("SERVER_NAME")
sname = ucase(sname)
if InStr(sname,"APPLE") <> 0 then
response.redirect "appledir"
elseif InStr(sname,"BANANA") <> 0 then
response.redirect "bananadir"
end if
%>

If you have only two domains and you still need the primary domain to point to the root of the site then you can use this code:

<%
sname = Request.ServerVariables("SERVER_NAME")
sname = ucase(sname)
if InStr(sname,"DOMAIN2") <> 0 then
response.redirect "directory2"
else
response.redirect "index.html"
end if
%>

You can substitute the "index.html" to whatever your default page is.
------------------------------------------------------------------------------------------------------------------
How to redirect domains / subdomains?
By default, all domain names and subdomain names are sent to the default page located inside the webroot folder. Due to the?nature of shared hosting plans, multiple domain names and sub domain names need to be redirected manually through code if you wish for them to show different content.


Resolution
Use one of the following code samples in your default page to redirect visitors to different locations depending on the domain they type in.

The following is an example of ASP code. You will need to save the page as default.asp or index.asp.
This code sample will *include* content from another page, leaving the URL in the URL bar of the web browser alone.

<%

SiteNameURL = Request.ServerVariables("SERVER_NAME")

 

Select Case SiteNameURL

Case "yourdomain.com" %><!-- #Include File="page1.asp" --><%

 

Case "www.yourdomain.com"

%><!-- #Include File="page1.asp" --><%

 

Case "yourotherdomain.com"

%><!-- #Include File="page2.asp" --><%

 

Case "www.yourotherdomain.com"

%><!-- #Include File="page2.asp" --><%

 

Case "subdomain1.yourdomain.com"

%><!-- #Include File="page3.asp" --><%

 

Case "subdomain2.yourdomain.com"

%><!-- #Include File="page4.asp" --><%

 

Case Else 'redirecting everything other than cases selected above

%><!-- #Include File="other.asp" --><%

End Select

%>



The following is an example of ASP code. You will need to save the page as default.asp or index.asp.
This code sample will redirect visitors to the corresponding page, changing the URL, for example http://domain.com/page1.asp.

<%

SiteNameURL =? Request.ServerVariables("SERVER_NAME")

 

Select Case SiteNameURL

Case "yourdomain.com"

Response.Redirect "page1.asp"

 

Case "www.yourdomain.com"

Response.Redirect "page1.asp"

 

Case "yourotherdomain.com"

Response.Redirect "page2.asp"

 

Case "www.yourotherdomain.com"

Response.Redirect "page2.asp"

 

Case "subdomain1.yourdomain.com"

Response.Redirect "page3.asp"

 

Case "subdomain2.yourdomain.com"

Response.Redirect "page4.asp"

 

Case Else 'redirecting everything other than cases selected above

Response.Redirect "other.asp"

End Select

%>



The following is an example of PHP code. You will need to save the page as default.php or index.php.
This code sample will redirect visitors to the corresponding page, changing the URL, for example http://domain.com/page1.php.
All domain names entries should be lower case.

<?

$SiteNameURL = $_SERVER['HTTP_HOST'];

 

switch (strtolower($SiteNameURL)) {

 

case "domain.com": //MUST BE LOWER CASE

 

header("Location: http://" . $_SERVER['HTTP_HOST'] . "/page1.php");

break;

 

case "www.domain.com": //MUST BE LOWER CASE

 

header("Location: http://" . $_SERVER['HTTP_HOST'] . "/page1.php");

break;

 

case "yourotherdomain.com": //MUST BE LOWER CASE

 

header("Location: http://" . $_SERVER['HTTP_HOST'] . "/page2.php");

break;

 

case "www.yourotherdomain.com": //MUST BE LOWER CASE

 

header("Location: http://" . $_SERVER['HTTP_HOST'] . "/page2.php");

break;

 

case "subdomain1.domain.com": //MUST BE LOWER CASE

 

header("Location: http://" . $_SERVER['HTTP_HOST'] . "/page3.php");

break;

 

case "subdomain2.domain.com": //MUST BE LOWER CASE

 

header("Location: http://" . $_SERVER['HTTP_HOST'] . "/page4.php");

break;

 

default:

header("Location: http://" . $_SERVER['HTTP_HOST'] . "/other.php");

 

}

?>



The following is an example of PHP code. You will need to save the page as default.php or index.php.
This code sample will *include* content from another page, leaving the URL in the URL bar of the web browser alone.
All domain names entries should be lower case.

<?

$SiteNameURL = $_SERVER['HTTP_HOST'];

 

switch (strtolower($SiteNameURL)) {

 

case "domain.com": //MUST BE LOWER CASE

include 'page1.php'

break;

 

case "www.domain.com": //MUST BE LOWER CASE

include 'page1.php'

break;

 

case "yourotherdomain.com": //MUST BE LOWER CASE

include 'page2.php'

break;

 

case "www.yourotherdomain.com": //MUST BE LOWER CASE

include 'page2.php'

break;

 

case "subdomain1.domain.com": //MUST BE LOWER CASE

include 'page3.php'

break;

 

case "subdomain2.domain.com": //MUST BE LOWER CASE

include 'page4.php'

break;

 

default:

include 'other.php'

 

}

?>



The following example uses JavaScript code. You will need to save the page as an HTML document, using .html or .htm as the file extension. This code sample will redirect visitors to the corresponding page, changing the URL, for example http://domain.com/page1.php.

<html>

<head>

var whois=location+" "

 

if (whois.indexOf("yourdomain.com") != -1)

{ window.location ="page1.html" }

 

if (whois.indexOf("www.yourdomain.com") != -1)

{ window.location ="page1.html" }

 

if (whois.indexOf("yourotherdomain.com") != -1)

{ window.location ="page2.html" }

 

if (whois.indexOf("www.yourotherdomain.com") != -1)

{ window.location ="page2.html" }

 

if (whois.indexOf("subdomain1.yourdomain.com") != -1)

{ window.location ="page3.html" }

 

if (whois.indexOf("subdomain2.yourdomain.com") != -1)

{ window.location ="page4.html" }

 

</head>

<body>

</body>

</html>

 

 

domainredirect subdomain

Tags: -

Related entries:

Last update: 2009-12-21 11:14
Author: Administrator
Revision: 1.1

TechNetSource on Facebook
| More