Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.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
Apache htaccess-返回404而不是403_Apache_.htaccess_Http Headers_Http Status Code 404 - Fatal编程技术网

Apache htaccess-返回404而不是403

Apache htaccess-返回404而不是403,apache,.htaccess,http-headers,http-status-code-404,Apache,.htaccess,Http Headers,Http Status Code 404,我正在用apache2在raspbian上运行一个Web服务器 在/var/www/html/resources/上,有一些文件(例如用户名、密码),但我不想让任何人知道它们存在,所以我想返回404(未找到)状态码,而不是403(禁止) (403错误会告诉任何人他在正确的道路上……) 是的,我读过很多问题,比如,这个问题下面的大部分链接正是我所需要的,但对我来说不起作用 我是否必须更改文件(httpd.conf,…)中的某些内容 以下是我所有返回404而不是403的尝试。 如果我尝试: ##

我正在用apache2在raspbian上运行一个Web服务器

在/var/www/html/resources/上,有一些文件(例如用户名、密码),但我不想让任何人知道它们存在,所以我想返回404(未找到)状态码,而不是403(禁止)

(403错误会告诉任何人他在正确的道路上……)

是的,我读过很多问题,比如,这个问题下面的大部分链接正是我所需要的,但对我来说不起作用

我是否必须更改文件(httpd.conf,…)中的某些内容

以下是我所有返回404而不是403的尝试。
如果我尝试:

## return 404 for each .htaccess
RedirectMatch 404 ".*\/\..*"
在/var/www/html/.htaccess中,如果访问//localhost/.htaccess,我得到的是403而不是404


导致内部服务器错误(500)
如果我试过这个:

# Will raise a 404 error, because the file <fake_file_for_apache_404.php> doesn't exist.
# We change 403 to 404 !
ErrorDocument 403 /fake_file_for_apache_404.php
# We need to rewrite 404 error, else we will have "fake_file_for_apache_404.php not found"
ErrorDocument 404 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\"><html><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>The requested URL <script type=\"text/javascript\">document.write(document.location.pathname);</script> was not found on this server.</p></body></html>"
#将引发404错误,因为该文件不存在。
#我们把403换成404!
ErrorDocument 403/fake_file_for_apache_404.php
#我们需要重写404错误,否则会出现“找不到apache_404.php的伪文件”
ErrorDocument 404“404未找到未找到请求的URL文档。write(document.location.pathname);在此服务器上未找到。

我得到了403和404(403-我想访问的文件被禁止,404-我应该重定向的文件找不到。)



那么我做错了什么呢?

可以用Perl编写。因此,在机密目录中,在.htaccess中:

ErrorDocument 403 /cgi-bin/fake404.cgi
提到的/cgi-bin/fake404.cgi:

#!/usr/bin/perl -w

use strict;

print 'Status: 404 Not Found
Content-Type: text/html; charset=UTF-8'."\n\n";

print '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
'."<p>The requested URL $ENV{REQUEST_URI} was not found on this server.</p>\n</body></html>\n";
#/usr/bin/perl-w
严格使用;
“打印”状态:404未找到
内容类型:text/html;字符集=UTF-8'。“\n\n”;
印刷品
404找不到
找不到
“在此服务器上找不到请求的URL$ENV{REQUEST_URI}”。

\n\n”;
我测试了上面的Perl代码:它按预期工作

另外,对于非机密目录中的机密文件,在.htaccess es中使用相同的指令,但在of块中使用

#!/usr/bin/perl -w

use strict;

print 'Status: 404 Not Found
Content-Type: text/html; charset=UTF-8'."\n\n";

print '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
'."<p>The requested URL $ENV{REQUEST_URI} was not found on this server.</p>\n</body></html>\n";