问题原文
原帖链接 · 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.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
