我可以使用Global.asax的开始请求重定向一切,
从mydomain.domain到www.mydomain.domain?
如果这个是真的,我该怎么做?
对Jan的回答进行了一些细微的修改,这对我有用:
protected void Application_BeginRequest(Object sender, EventArgs e) { string currentUrl = HttpContext.Current.Request.Url.ToString().ToLower(); if (currentUrl.StartsWith("http://mydomain")) { Response.Status = "301 Moved Permanently"; Response.AddHeader("Location", currentUrl.Replace("http://mydomain", "http://www.mydomain")); Response.End(); } }
更改是使用BeginRequest事件并将currentUrl设置为HttpContext.Current.Request.Url而不是HttpContext.Current.Request.Path.看到:
http://www.mycsharpcorner.com/Post.aspx?postID=40
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(); } }