【发布时间】:2020-05-12 16:08:50
【问题描述】:
我在 web.config 中使用以下规则:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="rewrite">
<match url="([0-9]+)/([0-9]+)" />
<action type="Rewrite" url="index.php?year={R:1}&item={R:2}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
它有效。
但如果用户在第二个查询(item)中写了一个或多个字母,我需要显示 404 错误,例如1b,或1bla等。
它应该只允许一个(正)数字,并且可能是一个查询,如 ?utm_source=anothersite 或 ?ref=anothersite 等。
换句话说,url应该是:
www.mysite.com/{year}/{item}
其中 {year} 和 {item} 都是强制性的,只能是数字,不允许其他元素(例如:www.mysite.com/{year}/{item}/{somethingelse}),除了 ?key=value。
如何更改 match url 值以达到该结果?
谢谢。
【问题讨论】:
-
"但是如果用户在第二个查询(项目)中写入一个或多个字母,例如 1b 或 1bla 等,我需要显示 404 错误。您的 Web 应用程序接收到错误的输入,因此它应该重定向到 404 页面。这与 IIS 或重写规则无关。
-
@Lex Li - 在我的服务器上,如果用户写入,例如 www.mysite.com/1a/1,那么他会收到 404 错误,但如果他写入,例如 www.mysite。 com/1/1a 然后该 url 被接受 - 它不应该。这就是我需要的。
标签: iis url-rewriting web-config