user1556433
user1556433

Reputation:

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.

Upvotes: 29

Views: 92553

Answers (3)

Chuck Le Butt
Chuck Le Butt

Reputation: 48758

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.

Upvotes: 7

conceptdeluxe
conceptdeluxe

Reputation: 3893

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.

Upvotes: 27

Giacomo1968
Giacomo1968

Reputation: 26056

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.

Upvotes: 23

Related Questions