我正在使用此作为指南来处理 IIS 重写规则: https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module
抱歉-也在SO上询问过(https://stackoverflow.com/questions/61149139/iis-url-rewrite-rule-test-pattern-error-the-input-data-to-test-does-not-match-t) 但 Server Fault 可能更适合这类问题,因此在这里提问。
这是我的第一条规则,使用来自 Microsoft 文档的指南:
From: http://localhost/article/342/some-article-title
To: http://localhost/article.aspx?id=342&title=some-article-title
Enter into "Edit Inbound Rule"
Pattern: ^article/([0-9]+)/([_0-9a-z-]+)
Rewrite URL: article.aspx?id={R:1}&title={R:2}
Enter into "Test Pattern"
Input data to test: article/234/some-title
Pattern: ^article/([0-9]+)/([_0-9a-z-]+)
To: article.aspx?id=342&title=some-article-title
它运行良好,并且测试也运行良好 - 屏幕截图如下:
该规则在 web.config 文件中如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<directoryBrowse enabled="true" />
<rewrite>
<rules>
<rule name="a">
<match url="^article/([0-9]+)/([_0-9a-z-]+)" />
<action type="Rewrite" url="article.aspx?id={R:1}&title={R:2}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
但是,我想让规则反过来发挥作用,因此我在规则定义中尝试了以下内容:
From: http://localhost/article.aspx?id=342&title=some-article-title
To: http://localhost/article/342/some-article-title
Enter into "Edit Inbound Rule"
Pattern: ^article.aspx?id=([0-9]+)&title=([_0-9a-z-]+)
Rewrite URL: article/([0-9]+)/([_0-9a-z-]+)
Enter into "Test Pattern"
Input data to test: article.aspx?id=342&title=some-article-title
To: article/234/some-title
不幸的是,这不起作用——当我测试模式时,我收到“要测试的输入数据与模式不匹配”的响应。
我尝试让规则反过来发挥作用的屏幕截图:
这是 web.config 文件中第二条规则的样子:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<directoryBrowse enabled="true" />
<rewrite>
<rules>
<rule name="a">
<match url="^article.aspx?id=([0-9]+)&title=([_0-9a-z-]+)" />
<action type="Rewrite" url="article/([0-9]+)/([_0-9a-z-]+)" appendQueryString="false" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
对于第二条规则,我尝试了勾选和不勾选“附加查询字符串”选项的情况(上面的摘录是勾选该选项的情况)。
我想知道我可能做错了什么?
答案1
这是因为您必须在模式中逃避?
这样的情况: 。\?
这是正确的版本:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<directoryBrowse enabled="true" />
<rewrite>
<rules>
<rule name="a">
<match url="^article.aspx\?id=([0-9]+)&title=([_0-9a-z-]+)" />
<action type="Rewrite" url="article/([0-9]+)/([_0-9a-z-]+)" appendQueryString="false" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>