繁体   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