2012-05-20 18 views
8

私はページごとに行うのではなく、ウェブサイトのURLリダイレクトを実装しようとしています。 global.asaxファイルでやりたい以下は私が定義したコードです。asp.net 4.0の301リダイレクト

私のメインURLとしてhttp://website.netを持っていたいと思っています& http://www.website.netと入力した場合、永続的なURLリダイレクトが必要です。

残念ながら、ライブウェブサイトでは機能しません。誰でもコードの問題を指摘できますか?コードはエラーを生成しません。

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")); 
    } 

} 

答えて

13

主な問題:あなたはApplication_Startで上記のことをしています - これは一度だけ実行されます。あなたはそれぞれの要求に繋がるべきです。これを試してみてください:

void Application_BeginRequest(object sender, EventArgs e) 
{ 
    // Code that runs on every request 

    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")); 
    } 

} 

アンより良いアプローチはWeb.Config内から設定することができ、URL書き換えを使用することです:.NETの

Microsoft rewriting module - Force www on url Or remove www from url

+0

not worki ng ...&何もエラーはありません – Learning

+0

私の間違い。気づいているはずです...私は、書き換えよりもむしろURLルーティングを実装する予定です。構造とnoのため書き換えに問題がありました。特定のページのクエリ文字列のあなたの返事を感謝します。ありがとう – Learning

5

バージョン4は、実際には単一のための改善された機能を持っていますページ実装 - redirectpermanent

Response.RedirectPermanent(NEW_URL);

8

IIS 7以降を使用している場合、最も簡単な解決策は、あなたのweb.configファイルにhttpRedirect要素を使用することです。

<httpRedirect enabled="true" exactDestination="true" httpResponseStatus="Permanent"> 
    <add wildcard="/MyOldAspFile.aspx" destination="/MyNewFile.aspx" /> 
    <add wildcard="/MyOldHtmlFile.html" destination="/MyNewFile.aspx" /> 
</httpRedirect> 

この方法は、あなたがドメインを変更しましたが、ページが同じであれば、たとえば、あなただけ追加する必要があり、非常に強力です:

<system.webServer> 
    <httpRedirect enabled="true" childOnly="true" destination="http://www.mynewdomain.com/" /> 
</system.webServer> 

私はここに小さな記事を書いた:ASP.NET 301 permanent redirects: the best solution

3

ここでは、いくつかの具体的な例を示します。あなたが古いページを削除したいと思っていたら(私のように)、いくつかのオプションがあります。

OPTION 1:Global.asaxの

void Application_BeginRequest(object sender, EventArgs e) 
    { 
     // Add permanent redirection for retired pages 
     if (Request.Url.LocalPath.ToLower().StartsWith("/[OLD PAGE NAME]")) 
     { 
      Response.RedirectPermanent("/[NEW PAGE NAME]", false); 
     } 
    } 

OPTION 2変更:アプリケーション・ドメイン名が何であるかを知らなかった場合

<system.webServer> 
    <httpRedirect enabled="true" httpResponseStatus="Permanent"> 
     <add wildcard="/[OLD PAGE NAME]" destination="/[NEW PAGE NAME]" /> 
    </httpRedirect> 
</system.webServer>  
0

web.configファイルを変更し、このようなものを使用する

protected void Application_BeginRequest(object sender, EventArgs e) 
{ 
    if(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority).Contains("localhost"))return; 
    var leftPartOfUrl = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority).ToLower(); 
    if (leftPartOfUrl.StartsWith("http") && leftPartOfUrl.Split('.').Length == 1) 
    { 
     var fullUrl = HttpContext.Current.Request.Url.ToString(); 
     HttpContext.Current.Response.Status = "301 Moved Permanently"; 
     HttpContext.Current.Response.AddHeader("Location", fullUrl.Insert(fullUrl.IndexOf("://", StringComparison.Ordinal) + 3, "www.")); 
    } 

}