Question

I have defined a URL Rewrite rule through IIS. Basically it turns something like this:

Article.aspx?ID=1&FriendlyURL=whatever

INTO

/1/whatever

Please note that Redirection is working right, but URL Rewrite (links within the page) are not being translated unless I am inside the Article.aspx page.

How can I make the Rewrite Rule apply to all the pages instead of only one? I'm posting below the written rules from Web.Config for your reference. Thanks.

<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>
Était-ce utile?

La solution

So I finally had to hard-code the links to be url-friendly by setting the "href" attribute within the code.

Something like this:

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

Thanks.

Autres conseils

I like regular expressions problems, try this.

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

The problem is in your OutboundRewrite rule's regular expression. I suggest you get a regex tool like expresso (my favorite), start with a very simple regex, then add complexity as your situation dictates.

The simplest regex that will match your example is:

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

Here's an example. Godspeed.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top