ASP.NET MVC Routing: Reroute complete domain ( from inside )
i want to route ALL requests in which the domain name "xyz.com" accours, like:
http://xyz.com/asdf/1
http://www.xyz.c开发者_如何学Com/asdf/1
http://www.xyz.com/bcd/asd/2
simply to another domain:
http://www.abc.com
How do i achieve this with the route mechanism of MVC? RouteHandler? MapRoute wildcard?
Thanks in advance
You can't do this VIA mvc routing, but you do have a few options:
Global.asax
protected void Application_BeginRequest(){
if (Context.Request.Url.Contains("xyz.com"))
Response.Redirect(Context.Request.Url.ToString().Replace("xyz.com", "www.xyz.com"));
}
A response.redirect will still send a temporary 302 status code. If you want search engines to understand that your site has moved for SEO purposes you will want to make sure you use a 301.
Preferred
If you are using IIS7 you can also accomplish this with the URL Rewrite Module. http://www.iis.net/download/urlrewrite
Install it, and stick a rule in the web.config that looks something like this
<rule name="Forward XYZ to ABC" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^xyz\.com$" />
<add input="{HTTP_HOST}" pattern="^www.xyz\.com$" />
</conditions>
<action type="Redirect" url="http://www.abc.com/{R:1}" redirectType="Permanent" />
</rule>
Alternative
In the global.asax add the following
protected void Application_BeginRequest(){
if (Context.Request.Url.Contains("xyz.com"))
{
Response.StatusCode = 301;
Response.RedirectLocation = url;
Response.End();
}
}
精彩评论