ErrorDocument 404 /404.php is not working in .htac

2020-02-26 04:29发布

I have a .htaccess file in the root directory and also 404.php file there. Content of my .htaccess file is:

ErrorDocument 404 /404.php 

But when I am mis-spelling my url, 404.php is not opening. Instead I am getting following message:

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.

But when I tried ErrorDocument 404 google.com, it worked.

3条回答
我欲成王,谁敢阻挡
2楼-- · 2020-02-26 04:32

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:

ErrorDocument 404 http://mygreat.server/404.php 

Full details in the official Apache documentation here.

查看更多
劫难
3楼-- · 2020-02-26 04:52

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.

So what one need to do is find out the absolute path and set it accordingly.

查看更多
劫难
4楼-- · 2020-02-26 04:52

You use ErrorDocument like so:

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:

  1. A local URL to redirect to if the action begins with a "/".
  2. An external URL to redirect to if the action is a valid URL.
  3. Text to be displayed (if none of the above). (The text must be wrapped in quotes (") if it consists of more than one word.)

Eg.

Custom text: ErrorDocument 404 "Oops! We can't find that pesky file. Sorry."

Local path: ErrorDocument 404 /local/path/to/404.php

External URL: 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/.

The problem is most likely that your path to 404.php is wrong, and cannot be found by your server.

查看更多
登录 后发表回答