代码之家  ›  专栏  ›  技术社区  ›  Wassim Taher

IIS URL重写只能在一个页面中工作

  •  3
  • Wassim Taher  · 技术社区  · 11 年前

    我已经通过IIS定义了URL重写规则。基本上是这样的:

    Article.aspx?ID=1&FriendlyURL=whatever
    

    进入

    /1/whatever
    

    请注意,重定向正在正常工作,但URL重写(页面中的链接)不会被翻译,除非我在Article.aspx页面中。

    如何使“重写规则”应用于所有页面而不是仅应用于一个页面?我在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>
    
    3 回复  |  直到 11 年前
        1
  •  1
  •   Wassim Taher    11 年前

    因此,我最终不得不通过在代码中设置“href”属性,对链接进行硬编码,使其对url友好。

    类似这样的内容:

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

    谢谢

        2
  •  0
  •   Luiz Felipe    11 年前

    我喜欢正则表达式问题,试试这个。

    <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>
    
        3
  •  0
  •   Zachary Yates    11 年前

    问题出在OutboundRewrite规则的正则表达式中。我建议你用一个正则表达式工具,比如 expresso (我最喜欢的),从一个非常简单的正则表达式开始,然后根据您的情况增加复杂性。

    与您的示例相匹配的最简单正则表达式是:

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

    Here's an example 。祝你好运。