You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过URL Rewrite限制目录搜索,使目录请求返回404状态码

修改URL Rewrite规则以拦截目录搜索请求

你可以通过调整现有规则的条件逻辑,让目录搜索请求也返回404状态。以下是两种可行的修改方案:

方案一:基于原有规则扩展

在原规则的条件中添加目录判断逻辑,确保目录请求触发404:

<rewrite>
  <rules>
    <rule name="RequestBlockingRule" stopProcessing="true">
      <match url=".*" />
      <conditions logicalGrouping="MatchAny">
        <add input="{URL}" pattern="^/(.*)" />
        <add input="Html5Viewer1.html" negate="true" pattern="(.*).html" />
        <!-- 新增:匹配存在的目录请求 -->
        <add input="{IS_DIR}" pattern="^true$" />
      </conditions>
      <action type="CustomResponse" statusCode="404" statusReason="Resource NotFound" statusDescription="You do not have permission to view this directory or page using the credentials that you supplied." />
    </rule>
  </rules>
</rewrite>

关键调整:

  • 将条件组的logicalGrouping设为MatchAny,只要满足任一条件就触发规则
  • 添加{IS_DIR}内置变量判断:当请求路径是实际存在的目录时,该变量值为true,直接触发404

方案二:简化规则逻辑(更严谨)

如果你的需求是仅允许访问Html5Viewer1.html,其他所有请求(包括目录搜索)都返回404,可以简化规则为:

<rewrite>
  <rules>
    <rule name="RequestBlockingRule" stopProcessing="true">
      <match url=".*" />
      <conditions>
        <!-- 仅允许精准匹配Html5Viewer1.html的请求 -->
        <add input="{URL}" pattern="^/Html5Viewer1.html$" negate="true" />
      </conditions>
      <action type="CustomResponse" statusCode="404" statusReason="Resource NotFound" statusDescription="You do not have permission to view this directory or page using the credentials that you supplied." />
    </rule>
  </rules>
</rewrite>

这个写法更简洁,同时自动覆盖了所有非目标文件的请求,包括目录浏览、其他文件访问等场景。

内容的提问来源于stack exchange,提问作者Venoth01

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 03:26:07