簡體   English   中英

將多個301重定向合並為一個ASP.NET URL重寫模塊

[英]Combine several 301 redirects into one for ASP.NET URL Rewrite module

我試圖將在Global.asax中實現的復雜重定向邏輯移到經典ASP.NET網站中的IIS URL Rewrite模塊的一組規則中。 不久,邏輯必須重定向請求,例如

{http|https}://[www.]ourdomain.com/[Home/]Products/old-product-name/page.aspx

https://ourdomain.com/new-product-name/

(可選和可變部分在方括號和大括號中)。

我使用URL重寫規則實現的前3項主要內容如下:

<rule name="Redirect to HTTPS">
    <match url=".*" />
    <conditions>
        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
    </conditions>
    <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
<rule name="Canonical Host Name">
    <match url="(.*)" />
    <conditions>
        <add input="{HTTP_HOST}" negate="true" pattern="^ourdomain\.com$" />
    </conditions>
    <action type="Redirect" url="https://ourdomain.com/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Default Page">
    <match url="(.*)default.aspx" />
    <conditions>
        <add input="{REQUEST_URI}" negate="true" pattern="-default.aspx$" />
    </conditions>
    <action type="Redirect" url="{R:1}" redirectType="Permanent" />
</rule>

這些規則允許將http://www.ourdomain.com/product-name/default.aspx請求重定向到https://ourdomain.com/product-name

但是,瀏覽器開發人員工具報告說此轉換導致301重定向。 我試圖在Internet上找到重定向鏈問題的解決方案,並找到了這篇文章 閱讀后,我設法將規則重新編碼為只有最后的301重定向和URL重寫:

<rule name="Redirect to HTTPS">
    <match url=".*" />
    <conditions>
        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
    </conditions>
    <action type="Rewrite" url="_{REQUEST_URI}" redirectType="Permanent" />
</rule>
<rule name="Canonical Host Name">
    <match url="(.*)" />
    <conditions>
        <add input="{HTTP_HOST}" negate="true" pattern="^ourdomain\.com$" />
    </conditions>
    <action type="Rewrite" url="_{R:1}" redirectType="Permanent" />
</rule>
<rule name="Default Page">
    <match url="(.*)default.aspx" />
    <conditions>
        <add input="{REQUEST_URI}" negate="true" pattern="-default.aspx$" />
    </conditions>
    <action type="Rewrite" url="_{R:1}" redirectType="Permanent" />
</rule>

<rule name="FINAL REDIRECT" stopProcessing="true">
    <match url="^(_+)(.*)" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTP_METHOD}" pattern="GET" />
    </conditions>
    <action type="Redirect" url="https://ourdomain.com{R:2}" />
</rule>

但是,這種方法看起來很不自然。 由於這些解決方法帶有下划線字符,因此很難添加新規則。 也許,對一個請求的這種數種重寫操作可能會影響網站性能。

URL重寫模塊是否還有另一個301重定向鏈問題的更優雅的解決方案?

這不是IIS Url Rewrite的限制,而是網絡的工作方式。 重定向會將標頭發送到瀏覽器,告訴標頭應導航到另一個位置。 如果您有3條規則適用於相同的原始請求,而3條規則適用於重定向,那么無論您如何執行,都將始終獲得3條重定向:IIS Url Rewrite,Apache .htaccess或您自己的自定義代碼。

您實施的解決方案是將多個重定向更改為服務器內部重寫的“補丁”,以及對這些特殊重寫的最終“捕捉”,最后將它們轉換為唯一的重定向(BTW,如果您有任何真實頁面的話)與_分層,您將無法使用此額外規則訪問它)。 不錯,不會影響您的表現,但是很丑。

我不是SEO專家,但是我認為301鏈接可以到達最終的相似URL 幾乎不會對您的SEO造成任何傷害 (如果您擔心的話)(請特別閱讀最后一部分)。 作為301重定向,它們只會影響用戶的首次訪問,因此無論如何都不會損害您的加載時間或轉化。

但是,如果要避免這種情況,請嘗試為您的目的制定一條規則,一次完成所有轉換。 在您的情況下,您想對此進行轉換:

{http|https}://[www.]ourdomain.com/[Home/]Products/old-product-name/page.aspx

(我猜/Home/有時可以出現也可以不出現)

到這個:

https://ourdomain.com/new-product-name/

然后,您可以使用以下一條規則:

<rule name="New URLs!!" stopProcessing="true">
    <match url="^(Home/){0,1}Products/(.+?/).+.aspx" />
    <conditions logicalGrouping="MatchAny">
        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
        <add input="{HTTP_HOST}" negate="true" pattern="^ourdomain\.com$" />
    </conditions>
    <action type="Redirect" url="https://ourdomain.com/{R:2}" redirectType="Permanent" />
</rule>

然后您將用一個規則轉換所有內容(因此,只有一個重定向)。 匹配URL部分中的正則表達式將標識這種特定類型的URL(舊產品URL),而條件(匹配其中任何一個)將處理HTTP和域更改。 stopProcessing="true"將使其他規則繼續起作用,您將可以控制這種特定類型的URL。

請注意,我尚未測試此規則,也許它存在一些小問題。 但是你明白了...

在此之后,您將需要其他規則來處理其他不太特定的URL,例如您的常規HTTP到HTTPS或規范域規則。 但是,在您要解決的情況下,這將一步實現您想要的目標。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM