簡體   English   中英

減少鏈接的URL重定向[IIS / Regex / ReWrite]

[英]Reduce Chained URL Redirects [IIS/Regex/ReWrite]

我已經實現了一些在IIS / ColdFusion上組合的URL重定向工作正常但我無法處理一些正則表達式模式以減少不需要的重定向。

目前,IIS僅在URL中找到模板名稱時才處理單個重定向(最多)。 例如,IIS重定向URL,例如

http://example.com/index.cfm/something/pretty/?page=1

http://example.com/something/pretty/?page=1

例如,它只是從URL中刪除模板名稱,使其后面的所有內容保持不變。 根據我的申請,上面的最終URL是有效的。

但另外,如果在最終URL中找不到尾部斜杠(/),ColduFusion應用程序正在處理這種情況並在末尾附加正斜杠,然后重定向到以正斜杠(/)結尾的URL。查詢字符串(如果有)。 它適用於一些邏輯,以保持PATH_INFO和QUERY_STRING完整。 但實際上,在以下情況下會導致多次重定向。

[INIT] http://example.com/index.cfm/sport/badminton 

[Redirect 1] [IIS-301]  http://example.com/sport/badminton

[Redirect 2] [CF-301]  http://example.com/sport/badminton/

既然我想在IIS中處理所有這些並覆蓋一個規則中的所有情況,我就無法制作(或找到)可以執行此操作的正則表達式模式。

當前的IIS重定向模式

^index.cfm/(.*)$ 

我嘗試了各種各樣的最簡單的

^index.cfm/(.*[^/])$

但它不包含帶有QUERY_STRING的URL。 在制作正則表達式時,你可以把我當作真正的天真。

更新1:我發現該問題的正確術語是“鏈式”重定向,並在moz.com上發現了一篇文章,它處理了我上面提到的同一問題。 我想它應該可以工作,當我在我的服務器上更改規則時,我認為我應該用我找到的其他可能有這樣一個問題的人更新這個問題。 一旦我可以使用此解決方案來解決我的問題,我會立即更新。

很抱歉,我無法在這里得到答案,但是當我在moz.com上更新上述關於某篇文章的問題時,我已經實現了這種方法並成功從鏈式/多重定向中恢復。

以前我們的Web應用程序支持以下URL

https://www.example.com/index.cfm/something/pretty/

比我們在IIS中使用URL ReWriting並從URL中刪除index.cfm。 但我們遇到麻煩,有多個/鏈式重定向從非https,非www或沒有尾部斜線等重定向。

https://moz.com/blog/what-every-seo-should-know-about-iis#chaining

閱讀上面的文章后,我在IIS上實現了以下一套規則,現在處理我們之前在IIS和ColdFusion上分別處理的所有情況。

<rules>
<!-- rewrite url to furnish with prefix(_) to better match individual parts -->
<rule name="Remove index.cfm" stopProcessing="false">
    <match url="(.*?)/?index\.cfm/(.*)$" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTP_METHOD}" pattern="GET" />
    </conditions>
    <action type="Rewrite" url="_{R:2}" />
</rule>
<rule name="Add Trailing Slash" stopProcessing="false">
    <match url="(.*[^/])$" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
    </conditions>
    <action type="Rewrite" url="_{R:1}/" />
</rule>
<rule name="ToLower Everything in URL" enabled="true" stopProcessing="false">
    <match url="(.*)" ignoreCase="false" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTP_METHOD}" pattern="GET" />
        <add input="{R:1}" pattern="[A-Z]" ignoreCase="false" />
    </conditions>
    <action type="Rewrite" url="_{ToLower:{R:1}}" />
</rule>
<!-- Now redirect the final prefix-furnished URL  -->       
<!-- match if there is at least one (_) at the start of the furnished URL. Redirect to the final URL -->    
<rule name="http[non www] to https[www] redirect" stopProcessing="true">
    <match url="^(_*)(.*)" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTP_HOST}" pattern="^www\.yoursite\.com$" negate="true" />
        <add input="{HTTP_METHOD}" pattern="GET" />
        <add input="{SERVER_PORT}" pattern="80" />  
    </conditions>
    <action type="Redirect" url="https://www.example.org/{R:2}" />
</rule>
<rule name="http[www] to https[www] redirect" stopProcessing="true">
    <match url="^(_*)(.*)" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTP_METHOD}" pattern="GET" />
        <add input="{SERVER_PORT}" pattern="80" />  
    </conditions>
    <action type="Redirect" url="https://www.example.org/{R:2}" />
</rule>
<rule name="https[non www] to https[www] redirect" stopProcessing="true">
    <match url="^(_*)(.*)" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTP_HOST}" pattern="^www\.yoursite\.com$" negate="true" />
        <add input="{HTTP_METHOD}" pattern="GET" />
        <add input="{SERVER_PORT}" pattern="443" /> 
    </conditions>
    <action type="Redirect" url="https://www.example.org/{R:2}" />
</rule>
<!-- this rule is supposed to run final redirect if non above redirect rules occured -->
<rule name="http// redirect" enabled="true" stopProcessing="true">
    <match url="^(_+)(.*)" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTP_METHOD}" pattern="GET" />
    </conditions>
    <action type="Redirect" url="{R:2}" />
</rule>

<!-- now after failing/running all rules above when the IIS reaches at this point, it's the fully validated/funrished URL that qualifies to serve with response. Rewrite the URL to run index.cfm as a template -->
<rule name="URL ReWrite" enabled="true" stopProcessing="true">
    <match url="^(.*)$" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <add input="{REQUEST_FILENAME}" pattern="/admin" negate="true" />
    </conditions>
    <action type="Rewrite" url="index.cfm/{R:1}" />
</rule>

這些規則嚴格按照我們的要求,我們希望將所有請求路由到[https],您必須查看上面的moz.com文章以供參考。

希望這可以幫助別人。

暫無
暫無

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

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