未显示处于状态(200 OK)和某个页面的403代码

时间:2014-07-30 15:19:06

标签: apache .htaccess error-handling

以下代码适用于错误代码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]

2 个答案:

答案 0 :(得分:0)

要执行此操作,您可以使用

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

答案 1 :(得分:0)

保持你的.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
?>