iis - 在 IIS7 中重写 URL

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

我一直在关注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 图像/ 文件夹。

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

最佳答案

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

^
    (
        (?!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}&a={R:4} .

关于iis - 在 IIS7 中重写 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8819646/

相关文章:

.net - .NET 的 NewRelic 需要在注册表中将 COR_ENABLE_PROFILING 设置为 1

jquery - 多个ajax请求非常慢的mvc

asp.net - 当对象作为响应传递时如何设置 Content-Length

c# - Microsoft.Web.Administration(用于 IIS7 自动化)在 VS 2010 中不起作用

.net - 无法从 ASP.NET MVC 内部写入 Blob 存储

c# - ASP.NET Core 2.0 - 部署到 IIS

asp.net - 如何增加IIS中的请求超时?

.htaccess - 除了需要 HTTPS 的 4 个页面外,所有页面都默认为 HTTP 连接

java - Wicket - Apache URL 重写 Access-Control-Allow-Origin 问题

apache - 连续多个 RewriteCond 将子域重定向到新子域