【问题标题】:Not showing 403 code in status (200 OK) and some page在状态(200 OK)和某些页面中未显示 403 代码
【发布时间】:2014-07-30 15:19:06
【问题描述】:

以下代码适用于错误代码 404,但对于 403 代码不起作用。它不显示页面 403.html 并继续在状态中显示代码 403 而不是 200 OK。为什么?

Deny from adsl.eunet.rs 
RewriteEngine On
RewriteBase /

RewriteRule ^403\.html/?$ - [L]

# If the requested file doesnt exist
# and if the requested file is not an existing directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . 403.html [L]

【问题讨论】:

  • 对非文件或目录的请求执行最后一次重写。这与 404 处理相同。我可以知道您的代码是如何生成 403 的吗?
  • 拒绝来自 adsl.eunet.rs
  • 好的,如果您使用的是Deny from,那么您必须使用ErrorDocument 403 /403.html 指令。您可以在您的网站上编写 php 代码吗?
  • 我的虚拟主机上允许使用 PHP,但我不知道如何使用它以及做什么。

标签: apache .htaccess error-handling


【解决方案1】:

保持你的 .htaccess 像这样:

### Deny from adsl.eunet.rs 

ErrorDocument 403 /403.php
RewriteEngine On
RewriteBase /

RewriteCond %{REMOTE_ADDR} =194.247.196.20
RewriteRule !^(404|403)\. - [F]

RewriteRule ^(404|403)\. - [L]

# If the requested file doesnt exist
# and if the requested file is not an existing directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . 404.html [L]

然后在/403.php 里面用这个php代码开始:

<?php
   http_response_code ( 200 );
   # rest of your html code
?>

【讨论】:

【解决方案2】:

为此,您可以使用

ErrorDocument 403 /403.html # define the error document

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ - [F,L] # match anything, F means return 403 Forbidden, L - last

您可以查看您可以在此处设置的标志http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule 以及有关更改错误文档的更多信息:http://httpd.apache.org/docs/2.2/mod/core.html#errordocument

【讨论】:

  • 它不工作它显示 500 internat server error...知道为什么..?
  • 删除 cmets 并使用 L 而不是 END。
  • 存在问题 403 代码要重写,但它在状态代码中显示为 403 错误,我不想在状态 403 代码消息中看到我需要 200 OK。你能帮忙吗?
  • 对于被禁止的文件,您想返回 200 而不是 403?
  • 是 - 200 OK 被禁止的文件设置为 403。
猜你喜欢
  • 2016-03-20
  • 1970-01-01
  • 1970-01-01
  • 2016-06-27
  • 1970-01-01
  • 1970-01-01
  • 2018-05-25
  • 1970-01-01
  • 2018-02-20
相关资源
最近更新 更多