Welcome to WebmasterWorld Guest from 107.21.163.40

Forum Moderators: ocean10000

Message Too Old, No Replies

How to do asp 301 conditional redirect subdomain

     
9:19 pm on Jul 24, 2012 (gmt 0)

New User

5+ Year Member

joined:June 19, 2011
posts: 18
votes: 0


I have a directory set up in the root of my main site (IIS Web Server, site in asp) that is a subdomain for the main site. My question is how do I set up a conditional 301 redirect in asp so that when people type in sub.mainsite.com it automatically goes to this directory and shows the whole subdomain site.

I tried this at the top of the default.asp page for the main site:

<%
if Request.ServerVariables("SERVER_NAME") = "sub.example.com" then
Call Response.Redirect("http://sub.example.com/subdirectoryname/")
end if
%>

It redirects to the subdomain, but if you click on any link on the subdomain website it says 404 not found.

Any help would be greatly appreciated!

[edited by: Ocean10000 at 3:50 pm (utc) on Jul 25, 2012]
[edit reason] examplefied [/edit]

10:49 pm on July 24, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


What do you mean by "goes to"?

It's one of the most abused phrases quoted in questions here with (at least) three different meanings depending on what you actually want to do.
12:53 am on July 25, 2012 (gmt 0)

New User

5+ Year Member

joined:June 19, 2011
posts: 18
votes: 0


By 'goes to' I mean that any request for the sub.example.com is directed to the subdomain website files that are located in a separate directory from the main site.

A request for a subdomain shows the subdomain website.

[edited by: Ocean10000 at 3:50 pm (utc) on Jul 25, 2012]
[edit reason] examplefied [/edit]

1:01 am on July 25, 2012 (gmt 0)

Full Member from NZ 

10+ Year Member

joined:July 28, 2003
posts: 298
votes: 5


Open IIS Manager, create a new site correctly configured for the subdomain in question.

Then on the main site, click on the folder and configure the HTTP Redirect to the new subdomain.

You might want to use http://sub.example.com$S$Q in the "Redirect requests to this destination" so that ALL requests are passed including query strings.

[edited by: Ocean10000 at 3:49 pm (utc) on Jul 25, 2012]
[edit reason] broke hyperlink/examplefied [/edit]

9:33 am on Sept 19, 2012 (gmt 0)

New User

joined:Sept 19, 2012
posts: 7
votes: 0


To implement a conditional 301 redirect in ASP, two elements must be present at the top of the default document an if statement testing the incoming HTTP header and the 301 redirect then a conditional redirect is useful when subdomains must redirect to the content of a subfolder other than the document root
Here is the example coding
private void Page_Load(object sender, System.EventArgs e)
{
If (Request.ServerVariables["SERVER_NAME"] == "sub.domain.com") then
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","http://sub.domain.com/other_content/index.html");
end if
}
7:40 pm on Sept 19, 2012 (gmt 0)

Full Member from NZ 

10+ Year Member

joined:July 28, 2003
posts: 298
votes: 5


You should have response.end before the end if, as the header will be added by the entire script will be processed before actually sending it to the browser.
7:38 am on Nov 8, 2012 (gmt 0)

New User

joined:Oct 3, 2012
posts: 6
votes: 0


Thank you give me a information to 301 redirect sub domain.