首页 > 解决方案 > 如何使用 web.config 覆盖同名但扩展名不同的文件的重定向规则?

问题描述

我有一个现有的 web.config 文件来将标题为 privacy.asp 的页面重定向到隐私(没有 .asp 扩展名)。它可以正常工作。

我现在有一个客户,他有一个文件privacy.html,我想发送一个永久的httpResponseStatus,即/privacy。

这是我尝试过的,但是当拉起privacy.html时,它会使用.html扩展名解析,但实际页面是.asp的页面。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

<location path="privacy.html">
<system.webServer>
<httpRedirect enabled="true" destination="/privacy/" httpResponseStatus="Permanent" />
</system.webServer>
</location>


<system.webServer>
<rewrite>
<rules>
<rule name="privacy" stopProcessing="true">
<match url="^privacy" />
<action type="Rewrite" url="privacy.asp" />
</rule>
</rules>
</system.webServer>

</configuration>

标签: windowsiisweb-config

解决方案


您可以使用以下重写规则:

<rule name="privacy" enabled="true" stopProcessing="true">
                <match url="(.*)" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    <add input="{REQUEST_URI}" pattern="privacy.html$" />
                </conditions>
                <action type="Redirect" url="privacy" appendQueryString="false" />
            </rule>
            <rule name="rewrite rule" stopProcessing="true">
                <match url="privacy$" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                <action type="Rewrite" url="privacy.asp" appendQueryString="false" logRewrittenUrl="true" />
            </rule>

在此处输入图像描述

注意:不要忘记删除位置路径和旧的 URL 重写规则。

问候,贾尔帕


推荐阅读