大网数据广告活动
网站运营与故障问答

IIS7 rewrite rule triggers 404 rather than redirecting to new pagw

· 52CE 我爱测

We recently replaced most of our default pages from default.htm to default.asp. Rewrite rules work fine if it's a sub-folder, e.g. example.com/help/default.asp where the default.htm was deleted. If I type domain.com/help/default.…

问题原文

原帖链接 · SKidd · CC BY-SA 4.0

We recently replaced most of our default pages from default.htm to default.asp.

Rewrite rules work fine if it's a sub-folder, e.g. example.com/help/default.asp where the default.htm was deleted. If I type domain.com/help/default.htm, it correctly shows the new .asp page and no 404 because the default.htm no longer exists.

At the root entering example.com/default.htm does not redirect to default.asp instead sending the user to the 404 page we created.

Tried exact match vs. wildcard, no difference.

Why the 404 and no redirect?

<rule name="homepage" patternSyntax="Wildcard" stopProcessing="true">
<match url="*example.com/default.htm" ignoreCase="true" />
<action type="Redirect" url="http://www.example.com/default.asp" redirectType="Permanent" />
</rule>

UPDATE: This works on a subfolder redirect from htm to asp:

<rule name="downloadhome" patternSyntax="Wildcard" stopProcessing="true">
<match url="*download/default.htm" ignoreCase="true" />
<action type="Redirect" url="/download/default.asp" redirectType="Permanent" />
</rule>

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

<match url="*example.com/default.htm" ignoreCase="true" />

I think the url attribute matches against the URL-path only, not the hostname. So the above should read:

<match url="^default\.htm" ignoreCase="true" />

This matches "default.htm" in the document root, or use the pattern default\.htm to match "default.htm" anywhere.

This uses patternSyntax="ECMAScript" on the <rule> (to use regular expressions). Or just remove the patternSyntax attribute entirely, since this is the default option


To have just one rule for both scenarios: a request for the document root and a subdirectory then you could probably do something like the following instead:

<rule name="replacedefaulthtm" stopProcessing="true">
<match url="(.*)default\.htm$" ignoreCase="true" />
<action type="Redirect" url="http://www.example.com/{R:1}default.asp" redirectType="Permanent" />
</rule>

Where {R:1} is a backreference to the captured subpattern (.*) in the preceding rule.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2019-02-26)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。