我一直在关注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}&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/