[英]IIS URL Rewrite working in one page only
I have defined a URL Rewrite rule through IIS. 我已经通过IIS定义了URL重写规则。 Basically it turns something like this:
基本上它变成这样:
Article.aspx?ID=1&FriendlyURL=whatever
INTO 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. 请注意,Redirection工作正常,但除非我在Article.aspx页面内,否则不会翻译URL Rewrite(页面中的链接)。
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.
我发布在Web.Config的书面规则之下,供您参考。 Thanks.
谢谢。
<system.webServer>
<rewrite>
<outboundRules>
<rule name="OutboundRewriteUserFriendlyURL1" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^(.*/)Article\.aspx\?ID=([^=&]+)&(?:amp;)?FriendlyURL=([^=&]+)$" />
<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&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=([^=&]+)&FriendlyURL=([^=&]+)$" />
</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}&FriendlyURL={R:2}" />
</rule>
</rules>
</rewrite>
</system.webServer>
So I finally had to hard-code the links to be url-friendly by setting the "href" attribute within the code. 因此,我最终必须通过在代码中设置“href”属性来将链接硬编码为友好的URL。
Something like this: 像这样的东西:
<a href='/1/hello-world/'>Read the "Hello World" Article</a>
Thanks. 谢谢。
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;)?FriendlyURL=([^=&]+)$" />
<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;)?FriendlyURL=([^=&]+)$" />
<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&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=([^=&]+)&FriendlyURL=([^=&]+)$" />
</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}&FriendlyURL={R:3}" />
</rule>
</rules>
</rewrite>
<urlCompression doStaticCompression="false"
doDynamicCompression="false" />
</system.webServer>
The problem is in your OutboundRewrite rule's regular expression. 问题出在您的OutboundRewrite规则的正则表达式中。 I suggest you get a regex tool like expresso (my favorite), start with a very simple regex, then add complexity as your situation dictates.
我建议你得到一个像expresso (我最喜欢的)一样的正则表达式工具,从一个非常简单的正则表达式开始,然后根据你的情况决定增加复杂性。
The simplest regex that will match your example is: 与您的示例匹配的最简单的正则表达式是:
Article\.aspx\?ID=(\d)&FriendlyURL=(.*)
Here's an example . 这是一个例子 。 Godspeed.
一帆风顺。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.