14

ページごとに行うのではなく、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"));
    }

}
4

5 に答える 5

17

主な問題:あなたは上記のことを-でやっています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"));
    }

}

さらに良いアプローチは、URL書き換えを使用することです。これは、以下から構成できますWeb.Config

Microsoftの書き換えモジュール-URLにwwwを強制するか、URLからwwwを削除します

于 2012-05-20T12:14:22.263 に答える
13

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パーマネントリダイレクト:最良の解決策

于 2012-12-11T20:32:01.163 に答える
8

.NETのバージョン4には、実際にはシングルページ実装用の改善された機能(redirectpermanent )があります。

Response.RedirectPermanent(NEW_URL);

于 2012-05-30T06:13:54.103 に答える
5

以前の正解と役立つ回答に基づいて、ここにいくつかの具体的な例を示します。(私が行ったように)古いページを削除したい場合は、いくつかのオプションがあります。

オプション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);
        }
    }

オプション2:web.configを変更します

<system.webServer>
    <httpRedirect enabled="true" httpResponseStatus="Permanent">
        <add wildcard="/[OLD PAGE NAME]" destination="/[NEW PAGE NAME]" />
    </httpRedirect>
</system.webServer>    
于 2014-01-08T17:00:15.323 に答える
2

アプリケーションのドメイン名がわからない場合は、次のようなものを使用してください

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.StatusCode = 301;
            HttpContext.Current.Response.AddHeader("Location", fullUrl.Insert(fullUrl.IndexOf("://", StringComparison.Ordinal) + 3, "www."));
            HttpContext.Current.Response.End();
        }
    }
于 2018-01-23T09:01:43.757 に答える