Welcome to WebmasterWorld Guest from

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)

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 Jul 24, 2012 (gmt 0)

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

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 Jul 25, 2012 (gmt 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 Jul 25, 2012 (gmt 0)

10+ Year Member

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 Sep 19, 2012 (gmt 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";
end if


7:40 pm on Sep 19, 2012 (gmt 0)

10+ Year Member

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)

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

Featured Threads

Hot Threads This Week

Hot Threads This Month