【问题标题】:Rewriting URLs in IIS7在 IIS7 中重写 URL
【发布时间】:2012-02-07 19:56:42
【问题描述】:

我一直在关注http://learn.iis.net/page.aspx/806/seo-rule-templates/,这是在 IIS7 中创建 SEO 友好 URL 的近乎完美的指南。

我有一个问题:

如果我创建一个规则来重写www.domain.com/contact/,我会进入 web.config:

<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="?p={R:1}" />
</rule>

但是我不能这样做www.domain.com/contact/send/

如果我创建一个规则来重写www.domain.com/contact/send/,我会进入 web.config:

<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="?p={R:1}&amp;a={R:2}" />
</rule>

但是我不能这样做www.domain.com/contact/

这两条规则都是为了让我在我的/scripts//css//images/ 文件夹中看不到任何脚本、css og 图像。

我如何制定规则以匹配 AND 不匹配上述 3 个文件夹?

【问题讨论】:

    标签: iis iis-7 url-rewriting web-config


    【解决方案1】:

    您的规则可能是这样的(为了更容易理解和最终编辑,我对其进行了扩展和评论):

    ^
        (
            (?!css|scripts|images)  # The directories you don't want to match
            [^/]+                   # The matched directory
        )
        (
            /
            (
                ([^/]+)             # Optional part
                /?                  # Optional trailing slash
            )?
        )?
    $
    

    翻译成以下内容:

    <match url="^((?!css|scripts|images)[^/]+)(/(([^/]+)/?)?)?$" />
    

    由于新正则表达式的捕获次数发生变化,因此重写 url 应更新为:?p={R:1}&amp;amp;a={R:4}

    【讨论】:

    • 太完美了.. 非常感谢! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-30
    • 2011-02-07
    • 2023-03-17
    • 2013-04-09
    • 2012-01-01
    • 2016-10-06
    相关资源
    最近更新 更多