从域到www.domain的全局301重定向
问题描述:
我可以使用Global.asax的begin请求重定向所有内容,
could i use the begin request of Global.asax to redirect everything,
从 mydomain.domain 到 www.mydomain.domain ?
如果这是真的,我该怎么办?
If this one is true, how can i do that?
答
protected void Application_PreRequestHandlerExecute(Object sender, EventArgs e)
{
string currentUrl = HttpContext.Current.Request.Path.ToLower();
if(currentUrl.StartsWith("http://mydomain"))
{
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", currentUrl.Replace("http://mydomain", "http://www.mydomain"));
Response.End();
}
}