Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net Can';t使用URL重写出站规则更改IIS响应代码_Asp.net_Iis_Url Rewriting_Web Config_Url Rewrite Module - Fatal编程技术网

Asp.net Can';t使用URL重写出站规则更改IIS响应代码

Asp.net Can';t使用URL重写出站规则更改IIS响应代码,asp.net,iis,url-rewriting,web-config,url-rewrite-module,Asp.net,Iis,Url Rewriting,Web Config,Url Rewrite Module,我正在尝试设置一个IIS URL重写规则,以匹配403个响应,因为有人试图在禁用目录浏览时浏览目录。然后我想将它们重定向到我为404s定义的常用ASP.NET自定义错误页面 以下是我目前的情况: <outboundRules> <!-- By default, browsing a directory with no default resource will return 403 --> <rule name="Directory browsing loc

我正在尝试设置一个IIS URL重写规则,以匹配403个响应,因为有人试图在禁用目录浏览时浏览目录。然后我想将它们重定向到我为404s定义的常用ASP.NET自定义错误页面

以下是我目前的情况:

<outboundRules>
  <!-- By default, browsing a directory with no default resource will return 403 -->
  <rule name="Directory browsing location">
    <match serverVariable="RESPONSE_LOCATION" pattern="(.*)" />
    <conditions>
      <add input="{RESPONSE_STATUS}" pattern="^403" />
    </conditions>
    <action type="Rewrite" value="/Error/PageNotFound?aspxerrorpath={PATH_INFO}"/>
  </rule>
  <rule name="Directory browsing status code" patternSyntax="ExactMatch">
    <match serverVariable="RESPONSE_STATUS" pattern="403" />
    <action type="Rewrite" value="302" />
  </rule>
</outboundRules>

我的假设是,它需要是一个出站规则,我需要重写状态代码并添加位置响应头,尽管后者在原始403响应中根本不存在

目前的行为是。。。没有什么。无论我做了多少调整,我仍然看到403。有什么想法吗


顺便说一句,没有,网站上没有任何合法的403会因此而被吞没。我还可以为每个可能导致满足条件的路径创建入站规则,但这不是很容易扩展的。

我也不太走运,但我怀疑这个响应中可能有提示

引用他们的回答:“然而,在503情况下,请求永远不会到达worker进程,503响应直接来自http.sys。”


我怀疑403可能永远不会进入IIS进程,也无法重写。

我记得在SharePoint出现此问题时,我们向域名注册商查询我们的域名,并提交DNS记录以转发内容(我认为它们是CNAME记录)。这是一个混乱的保持同步,但这是唯一的方法,我们可以让它工作。IIS中的HTTP和URL重写在某些情况下至少在SharePoint中不起作用。

不确定这是否有帮助,因为这不是重写规则,但这将使用web.config的httpErrors部分强制403返回错误页:

<configuration>
    <system.web>
      <compilation debug="false" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
      <customErrors defaultRedirect="~/errorpage.html" mode="On">
      </customErrors>
    </system.web>
  <system.webServer>
    <httpErrors>
      <remove statusCode="404" subStatusCode="-1" />
      <error statusCode="404" prefixLanguageFilePath="" path="/errorpage.html" responseMode="ExecuteURL" />
      <remove statusCode="403" subStatusCode="-1" />
      <error statusCode="403" prefixLanguageFilePath="" path="/errorpage.html" responseMode="ExecuteURL" />
    </httpErrors>
    <defaultDocument>
      <files>
        <remove value="default.aspx" />
        <remove value="iisstart.htm" />
        <remove value="index.htm" />
        <remove value="Default.asp" />
        <remove value="Default.htm" />
      </files>
    </defaultDocument>
  </system.webServer>
</configuration>

URL重写几乎可以处理所有内容,但不能处理HTTP状态代码,因为它位于响应头之外。所以不幸的是,URL重写不能用这个做任何事情,或者至少不是我所能找到的。我曾多次想做类似的事情。注意,您可以使用{RESPONSE_status}使用条件检查状态,但不能更新它

@RyanCEI的回复是我的建议。此外,您可以使用subStatusCode将错误范围限定为403.14,并且对于仅测试,请确保测试关闭框或将errorMode设置为自定义,因为默认情况下,IIS在本地框上测试时不会显示自定义错误页面

下面是一个同时执行这两个功能的配置示例

    <httpErrors errorMode="Custom">
        <error statusCode="403" subStatusCode="14" path="/errorpage.htm" responseMode="ExecuteURL" />
    </httpErrors>

测试后,您可以关闭errorMode=“Custom”