我正在尝试为网站实现 URL 重定向,而不是逐页进行。我想在 global.asax 文件中执行此操作。下面是我定义的代码。
我想将http://website.net作为我的主要网址,并且如果有人输入 http://www.website.net ,我希望有一个永久的网址重定向。
不幸的是,它不适用于实时网站。谁能指出代码中的问题。该代码不会产生任何错误。
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("http://website.net"))
{
HttpContext.Current.Response.Status = "301 Moved Permanently";
HttpContext.Current.Response.AddHeader("Location", Request.Url.ToString().ToLower().Replace("http://website.net", "http://www.website.net"));
}
}