簡體   English   中英

IIS URL重寫僅在一個頁面中工作

[英]IIS URL Rewrite working in one page only

我已經通過IIS定義了URL重寫規則。 基本上它變成這樣:

Article.aspx?ID=1&FriendlyURL=whatever

INTO

/1/whatever

請注意,Redirection工作正常,但除非我在Article.aspx頁面內,否則不會翻譯URL Rewrite(頁面中的鏈接)。

如何使重寫規則適用於所有頁面而不是僅適用於所有頁面? 我發布在Web.Config的書面規則之下,供您參考。 謝謝。

<system.webServer>
    <rewrite>
        <outboundRules>
            <rule name="OutboundRewriteUserFriendlyURL1" preCondition="ResponseIsHtml1">
                <match filterByTags="A, Form, Img" pattern="^(.*/)Article\.aspx\?ID=([^=&amp;]+)&amp;(?:amp;)?FriendlyURL=([^=&amp;]+)$" />
                <action type="Rewrite" value="{R:1}{R:2}/{R:3}/" />
            </rule>
            <preConditions>
                <preCondition name="ResponseIsHtml1">
                    <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
                </preCondition>
            </preConditions>
        </outboundRules>
        <rewriteMaps>
            <rewriteMap name="Article Rewrite">
                <add key="Article.aspx?ID=1&amp;FriendlyURL=whatever" value="/1/whatever" />
            </rewriteMap>
        </rewriteMaps>
        <rules>
            <rule name="RedirectUserFriendlyURL1" stopProcessing="true">
                <match url="^Article\.aspx$" />
                <conditions>
                    <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
                    <add input="{QUERY_STRING}" pattern="^ID=([^=&amp;]+)&amp;FriendlyURL=([^=&amp;]+)$" />
                </conditions>
                <action type="Redirect" url="{C:1}/{C:2}" appendQueryString="false" />
            </rule>
            <rule name="RewriteUserFriendlyURL1" stopProcessing="true">
                <match url="^([^/]+)/([^/]+)/?$" />
                <conditions>
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                </conditions>
                <action type="Rewrite" url="Article.aspx?ID={R:1}&amp;FriendlyURL={R:2}" />
            </rule>
        </rules>

    </rewrite>
</system.webServer>

因此,我最終必須通過在代碼中設置“href”屬性來將鏈接硬編碼為友好的URL。

像這樣的東西:

 <a href='/1/hello-world/'>Read the "Hello World" Article</a>

謝謝。

我喜歡正則表達式問題,試試這個。

<system.webServer>
    <rewrite>
        <outboundRules>
            <clear />
            <rule name="OutboundRewriteUserFriendlyURL1"
                  preCondition="ResponseIsHtml1">
                <match filterByTags="A, Form, Img"
                       pattern="^(.*/)([^\.]+)\.aspx\?ID=([^=&amp;]+)&amp;(?:amp;)?FriendlyURL=([^=&amp;]+)$" />
                <conditions logicalGrouping="MatchAll"
                            trackAllCaptures="true" />
                <action type="Rewrite"
                        value="{R:1}{R:2}/{R:3}/{R:4}/" />
            </rule>
            <rule name="OutboundRewriteUserFriendlyURL2"
                  preCondition="ResponseIsHtml1">
                <match filterByTags="A, Form, Img"
                       pattern="^(.*)\?ID=([^=&amp;]+)&amp;(?:amp;)?FriendlyURL=([^=&amp;]+)$" />
                <conditions logicalGrouping="MatchAll"
                            trackAllCaptures="true" />
                <action type="Rewrite"
                        value="" />
            </rule>
            <preConditions>
                <preCondition name="ResponseIsHtml1">
                    <add input="{RESPONSE_CONTENT_TYPE}"
                         pattern="^text/html" />
                </preCondition>
            </preConditions>
        </outboundRules>
        <rewriteMaps>
            <rewriteMap name="Article Rewrite">
                <add key="Article.aspx?ID=1&amp;FriendlyURL=whatever"
                     value="/1/whatever" />
            </rewriteMap>
        </rewriteMaps>
        <rules>
            <rule name="RedirectUserFriendlyURL1"
                  stopProcessing="true">
                <match url="^([^\.]+)\.aspx$" />
                <conditions>
                    <add input="{REQUEST_METHOD}"
                         pattern="^POST$"
                         negate="true" />
                    <add input="{QUERY_STRING}"
                         pattern="^ID=([^=&amp;]+)&amp;FriendlyURL=([^=&amp;]+)$" />
                </conditions>
                <action type="Redirect"
                        url="{R:1}/{C:1}/{C:2}"
                        appendQueryString="false" />
            </rule>
            <rule name="RewriteUserFriendlyURL1"
                  stopProcessing="true">
                <match url="^([^/]+)/([^/]+)/([^/]+)/?$" />
                <conditions>
                    <add input="{REQUEST_FILENAME}"
                         matchType="IsFile"
                         negate="true" />
                    <add input="{REQUEST_FILENAME}"
                         matchType="IsDirectory"
                         negate="true" />
                </conditions>
                <action type="Rewrite"
                        url="{R:1}.aspx?ID={R:2}&amp;FriendlyURL={R:3}" />
            </rule>
        </rules>
    </rewrite>
    <urlCompression doStaticCompression="false"
                    doDynamicCompression="false" />
</system.webServer>

問題出在您的OutboundRewrite規則的正則表達式中。 我建議你得到一個像expresso (我最喜歡的)一樣的正則表達式工具,從一個非常簡單的正則表達式開始,然后根據你的情況決定增加復雜性。

與您的示例匹配的最簡單的正則表達式是:

Article\.aspx\?ID=(\d)&FriendlyURL=(.*)

這是一個例子 一帆風順。

暫無
暫無

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

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