如何将子文件夹重定向到Mod Rewrite for IIS 7.0中的查询? [英] How to redirect subfolder to query in Mod Rewrite for IIS 7.0?

查看:111
本文介绍了如何将子文件夹重定向到Mod Rewrite for IIS 7.0中的查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从iis.net使用Mod Rewrite for IIS 7.0并想要重定向请求:

I'm using Mod Rewrite for IIS 7.0 from iis.net and want to redirect requests:

  • http://example.com/users/foo to http://example.com/User.aspx?name=foo
  • http://example.com/users/1 to http://example.com/User.aspx?id=1

我有创建了2条规则:

<rule name="ID">
   <match url="/users/([0-9])" />
   <action type="Rewrite" url="/User.aspx?id={R:1}" />
</rule>
<rule name="Name">
   <match url="/users/([a-z])" ignoreCase="true" />
   <action type="Rewrite" url="/User.aspx?name={R:1}" />
</rule>

它将测试传递给iis mmc测试对话框,但不在调试中(URL像 http:// localhost:9080 / example.com / users / 1 ... / users / foo )并且不在真正的IIS上!

It passes a test into iis mmc test dialog, but doesn't in debug (URL like http://localhost:9080/example.com/users/1 or …/users/foo) and doesn't on real IIS!

我做错了什么?

推荐答案

显而易见的问题是,您当前的正则表达式只匹配用户名中的一个字符或一个数字。您需要在括号内添加加号量词,以匹配多个字母或数字。有关正则表达式量词的详细信息,请参见此页。请注意,您将不会匹配/ users /之类的普通URL(无ID或名称)。确保这是你想要的。

The obvious problem is that your current regexes only match one character in the user name or one number. You'll need to add a plus quantifier inside the parentheses in order to match multiple letters or numbers. See this page for more info about regex quantifiers. Note that you won't be matching plain URLs like "/users/" (no ID or name). Make sure this is what you intended.

你遇到的另一个问题是IIS从初始斜杠后的第一个字符开始评估重写规则。因此,匹配 / users /([0-9])的规则将无法匹配任何内容,因为在进行正则表达式评估时,URL看起来像 users / foo 不是 / users / foo 。解决方案是在模式的开头使用 ^ (这是表示字符串开头的正则表达式字符)而不是斜杠。像这样:

The other problem you're running into is that IIS evaluates rewrite rules starting from the first character after the initial slash. So your rule to match /users/([0-9]) won't match anything because when the regex evaluation happens, the URL looks like users/foo not /users/foo. The solution is to use ^ (which is the regex character that means "start of string") at the start of the pattern instead of a slash. Like this:

<rule name="ID">
    <match url="^users/([0-9]+)" />
    <action type="Rewrite" url="/User.aspx?id={R:1}" />
</rule>
<rule name="Name">
    <match url="^users/([a-z]+)" ignoreCase="true" />
    <action type="Rewrite" url="/Users.aspx?name={R:1}" />
</rule>

请注意,您选择 Users.aspx 其中一个URL和另一个 User.aspx (无复数)。确保这是你想要的。

Note that you're choosing Users.aspx for one of these URLs and User.aspx (no plural) for the other. Make sure this is what you intended.

BTW,我想出这些东西的方式是使用 IIS失败请求跟踪以解决重写规则。这使得诊断非常简单。我能够发出一个测试请求,并查看跟踪以找到每个重写规则的评估位置(它位于跟踪的一部分名为PATTERN_MATCH。对于您的一个规则的特定PATTERN_MATCH,我看到了:

BTW, the way I figured these things out was by using IIS Failed Request Tracing to troubleshoot rewrite rules. This made diagnosing this really easy. I was able to make a test request and look through the trace to find where each rewrite rule is being evaluated (it's in a section of the trace called "PATTERN_MATCH". For the particular PATTERN_MATCH for one of your rules, I saw this:


-PATTERN_MATCH

模式/用户/([0-9] +?)

InputURL用户/ 1

否定false

匹配false

-PATTERN_MATCH
Pattern /users/([0-9]+?)
InputURL users/1
Negate false
Matched false

注意缺少开始斜线。

这篇关于如何将子文件夹重定向到Mod Rewrite for IIS 7.0中的查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆