我已经通过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;)?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>
答案 0 :(得分:1)
所以我最终不得不通过在代码中设置“href”属性来将链接硬编码为友好的URL。
这样的事情:
<a href='/1/hello-world/'>Read the "Hello World" Article</a>
感谢。
答案 1 :(得分:0)
我喜欢正则表达式问题,试试这个。
<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>
答案 2 :(得分:0)
问题在于OutboundRewrite规则的正则表达式。我建议你使用expresso(我最喜欢的)这样的正则表达式工具,从一个非常简单的正则表达式开始,然后根据你的情况增加复杂性。
与您的示例匹配的最简单的正则表达式是:
Article\.aspx\?ID=(\d)&FriendlyURL=(.*)
Here's an example。一帆风顺。