简体   繁体   中英

IIS: How to redirect all subpages to other domain, but not the root domain

I have a question regarding redirect in IIS or web.config.

I have two domains:

domain1.com and domain2.com

When people are browsing to http://domain1.com , they land on that domain. But if they are browsing to any of the subpages on domain1.com, they are redirected to the same subpage on domain2.com. Lets' say that I go to domain1.com/my-subpage I want to redirect people to domain2.com/my-subpage

This means that the only time when people are landing on, and staying on, the domain1.com-domain is when they land directly on the root of the domain. On all other request to domain1.com/* they are redirected to domain2.com/*

I know how to do this in the IIS using the redirect module if it was the entire domain, including the frontpage, that should redirect. But I can't figure aout hos to get around this when the frontpage shall not redirect.

Hope that someone can help me out here.

Thanks in advance.

Regards, Kim

One way is on Global.asax and under the Application_BeginRequest you can make your redirect, here is an example:

protected void Application_BeginRequest(Object sender, EventArgs e)
{   
    // this is an example, change it base on your case..
    if(!HttpContext.Current.Request.Path.Contain("www.domain1.com/"))
       HttpContext.Current.Response.Redirect(
          Request.RawUrl.Replace("www.domain1.com/","www.domain2.com/")
             , true);   
}

This is not work for post back.

One second way, and work for post back also, and can work for you because you have only one page and all the rest links redirect to inside pages is to change the <form action=""....> on the default.aspx page, the first page of all.

On post back, you may have issues with security check of the asp.net forms, you must check that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM