[拒绝文件夹权限时如何显示自定义ErrorDocument?

问题描述 投票:0回答:1

我试图拒绝访问服务器上某个文件夹中的所有内容(运行Linux signature 4.14.117)。我将该文件夹的chmod设置为0700

enter image description here

并且运行良好(通过拒绝访问。)

尽管现在,不是我自己的403.php错误页面,它是在我的.htaccess根目录中定义的,当我尝试访问该文件夹中的任何内容时,都会得到此错误:

禁止,您无权访问此资源。服务器无法读取htaccess文件,拒绝访问是安全的

此外,尝试执行403禁止错误时,使用ErrorDocument处理请求。

enter image description here

我曾尝试向该文件夹添加.htaccess的较短版本,并将其访问权限设置为0644,但似乎没有任何改变。这是那个.htaccess中的内容:

Redirect 301 / https://example.com/blog

# Define priority of which index file is used first
DirectoryIndex index.php index.htm index.html

# Error pages:
ErrorDocument 401 /401.php
ErrorDocument 403 /403.php
ErrorDocument 404 /404.php
ErrorDocument 500 /500.php

在这种情况下,如何显示我的自定义403.php页面?

php linux .htaccess http-status-code-403
1个回答
2
投票

Apache试图从该子目录加载.htaccess文件,但是您的权限已禁止该文件查找。您可以在目录上使用chmod 755,然后在所有文件上使用chmod 600,因此您将得到以下内容:

0700 dir/
0600 dir/denied_file_1
0600 dir/denied_file_2

或在.htaccess中输入denies everything

0700 dir/
0644 dir/.htaccess
0600 dir/denied_file_1
0600 dir/denied_file_2

但是,如果您的目标只是完全拒绝对子文件夹的所有Web访问,那么最好的选择就是将文件夹移到Web服务器文档根目录之外。这样,您可以避免配置和权限错误。

[更新]

嗯,该文件夹将包含我的博客文章的模板。我不希望通过网络(直接)访问它们,但我需要它们可从我的.php脚本中读取。那是目标。

很好。即使Apache无法读取,PHP仍然可以读取Web根目录之外的文件。例如:

/path/to/your/dir/
/path/to/your/dir/src/ <-- put your library files here
/path/to/your/dir/public/  <-- point web server doc root here
/path/to/your/dir/public/index.php
/path/to/your/dir/templates/
/path/to/your/dir/templates/foo.template
/path/to/your/dir/templates/bar.template

至更改每个文件上的烫发,听起来有点乏味且容易错过。

正是为什么总是将它们放在doc根目录之外总是更好。

© www.soinside.com 2019 - 2024. All rights reserved.