ページごとに行うのではなく、WebサイトにURLリダイレクトを実装しようとしています。global.asaxファイルで実行したいと思います。以下は私が定義したコードです。
メインのURLとしてhttp://website.netを使用し、誰かがhttp://www.website.netと入力した場合に永続的なURLリダイレクトを使用したい。
残念ながら、ライブWebサイトでは機能していません。誰かがコードの問題を指摘できますか?コードはエラーを生成しません。
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"));
}
}