【问题标题】:Friendly URL with the same pattern具有相同模式的友好 URL
【发布时间】:2013-03-23 11:30:05
【问题描述】:

我可以重定向并创建一个友好的 URL:

<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
    <match url="^Product/Tour\.aspx$" />
        <conditions>
            <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
        </conditions>
        <action type="Redirect" url="Product/Tour" appendQueryString="false" />
</rule>

<rule name="RewriteUserFriendlyURL2" stopProcessing="true">
    <match url="^Product/Tour$" />
        <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        </conditions>
    <action type="Rewrite" url="Product/Tour.aspx" />
</rule>

但是,例如,如果我有:

http://www.domain.com/Product/Features.aspx
http://www.domain.com/Product/Download.aspx
http://www.domain.com/Product/FAQ.aspx

等等

我可以编写一条规则来为所有这些链接创建友好的 URL 以便接收吗?

http://www.domain.com/Product/Features
http://www.domain.com/Product/Download
http://www.domain.com/Product/FAQ

有几个链接很容易,但有很多规则很难维护。

【问题讨论】:

    标签: iis web-config rewrite friendly-url


    【解决方案1】:

    您可以使用正则表达式模式和back reference 来执行此操作:

    <rule name="RedirectUserFriendlyURL1" stopProcessing="true">
        <match url="^Product/([A-z0-9]+)\.aspx$" />
            <conditions>
                <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
            </conditions>
            <action type="Redirect" url="Product/{R:1}" appendQueryString="false" />
    </rule>
    
    <rule name="RewriteUserFriendlyURL2" stopProcessing="true">
        <match url="^Product/([A-z0-9]+)$" />
            <conditions>
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            </conditions>
        <action type="Rewrite" url="Product/{R:1}.aspx" />
    </rule>
    

    如果你有很多 url 需要重写,你也应该阅读关于 Rewrite Maps 的文档。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-11
      • 1970-01-01
      • 2011-03-26
      • 1970-01-01
      • 2012-07-04
      • 1970-01-01
      • 2012-06-05
      • 2018-08-12
      相关资源
      最近更新 更多