ErrorDocument 404 /404.php 在 PHP 中的 .htaccess 文件中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22976772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ErrorDocument 404 /404.php is not working in .htaccess file in PHP
提问by
I have a .htaccess
file in the root directory and also 404.php file there. Content of my .htaccess
file is:
我.htaccess
在根目录中有一个文件,那里还有 404.php 文件。我的.htaccess
文件内容是:
ErrorDocument 404 /404.php
But when I am mis-spelling my url, 404.php
is not opening. Instead I am getting following message:
但是,当我拼错我的网址时,404.php
无法打开。相反,我收到以下消息:
Not Found
The requested URL /mywebsite/ites.php was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
未找到
在此服务器上找不到请求的 URL /mywebsite/ites.php。
此外,在尝试使用 ErrorDocument 处理请求时遇到 404 Not Found 错误。
But when I tried ErrorDocument 404 google.com
, it worked.
但是当我尝试时ErrorDocument 404 google.com
,它起作用了。
采纳答案by conceptdeluxe
I'll consolidate my comments to this answer:
我将合并我对这个答案的评论:
When setting ...
设置时...
ErrorDocument 404 /404.php
the /404.php
path may not be the absolute path to your htdocs folder root but instead the root of your filesystem. This may be, based on your configuration, e.g. /home/htdocs/
or ~
and so on.
该/404.php
路径可能不是 htdocs 文件夹根目录的绝对路径,而是文件系统的根目录。这可能基于您的配置,例如/home/htdocs/
或~
等等。
So what one need to do is find out the absolute path and set it accordingly.
所以需要做的就是找出绝对路径并进行相应的设置。
回答by JakeGould
Where is your 404.php
actually located in relation to your .htaccess
file? Can you simply run it as a direct URL? Is the file readable by the server? Or is it in a nested subdirectory? You can also try the full URL as well:
404.php
相对于您的.htaccess
文件,您的实际位置在哪里?您可以简单地将其作为直接 URL 运行吗?服务器是否可以读取该文件?还是在嵌套的子目录中?您也可以尝试完整的 URL:
ErrorDocument 404 http://mygreat.server/404.php
Full details in the official Apache documentation here.
此处的官方 Apache 文档中的完整详细信息。
回答by Chuck Le Butt
You use ErrorDocumentlike so:
你像这样使用ErrorDocument:
ErrorDocument <3-digit-code> <action>
ErrorDocument <3-digit-code> <action>
You have three types of action that are triggered by what you type:
您输入的内容会触发三种类型的操作:
The the action will be treated as:
该操作将被视为:
- A local URL to redirect to if the action begins with a "/".
- An external URL to redirect to if the action is a valid URL.
- Text to be displayed (if none of the above). (The text must be wrapped in quotes (") if it consists of more than one word.)
- 如果操作以“/”开头,则重定向到的本地 URL。
- 如果操作是有效 URL,则要重定向到的外部 URL。
- 要显示的文本(如果以上都不是)。(如果文本包含多个单词,则必须用引号 (") 括起来。)
Eg.
例如。
Custom text:
ErrorDocument 404 "Oops! We can't find that pesky file. Sorry."
自定义文本:
ErrorDocument 404 "Oops! We can't find that pesky file. Sorry."
Local path:
ErrorDocument 404 /local/path/to/404.php
本地路径:
ErrorDocument 404 /local/path/to/404.php
External URL:
ErrorDocument 404 http://external_url.example.com/server_error.html
外部网址:
ErrorDocument 404 http://external_url.example.com/server_error.html
You've chosen a local file, but are probably not linking correctly from the server's perspective. Local server paths are not what you see in your URL, and often include things like ~/htdocs/www/domainname/
.
您选择了一个本地文件,但从服务器的角度来看可能没有正确链接。本地服务器路径不是您在 URL 中看到的内容,通常包括~/htdocs/www/domainname/
.
The problem is most likely that your path to 404.php
is wrong, and cannot be found by your server.
问题很可能是您的路径404.php
错误,并且您的服务器找不到。