简体   繁体   中英

ErrorDocument 404 /404.php is not working in .htaccess file in PHP

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.

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, eg /home/htdocs/ or ~ and so on.

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

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 .

You use ErrorDocument like so:

ErrorDocument <3-digit-code> <action>

The <3-digit-code> is a HTTP response status code (eg. "404").

And you have three types of <action> that are triggered by what you type:

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

For example:

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

External URL : ErrorDocument 404 http://external_url.example.com/server_error.html

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

You've chosen a local path, but are probably not pointing correctly to the file from the server's perspective. Note that 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.

Hey you can change some settings in httpd.conf from:-

<Directory />
AllowOverride none
Require all denied
</Directory>

to: -

<Directory />
AllowOverride All
Require all denied
</Directory>

The you can use

ErrorDocument 404 /404.php

In htaccess

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM