28

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.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
  • 3
    a 404 Not Found error was encountered while trying to use an ErrorDocument - So, your 404.php was a 404! Check logs to see where apache is looking for your 404.php – enapupe Apr 10 '14 at 01:02
  • You need an absolute path for `404.php` e.g. `/home/web/404.php` – conceptdeluxe Apr 10 '14 at 01:04
  • actually, it does work. I've exactly this in my .htacces and it works well: `ErrorDocument 404 /404.html` sorry: this is in response to conceptdeluxe above – Steve Horvath Apr 10 '14 at 01:08
  • @SteveHorvath - Actually it was working a long time ago. But when I tried it today, it is not working. –  Apr 10 '14 at 01:09
  • 1
    have you checked the access/error logs, what/where the apache is looking for? – Steve Horvath Apr 10 '14 at 01:11
  • What I ment is that, based on your configuration, `/` is actually not htdocs but e.g. `~` – conceptdeluxe Apr 10 '14 at 01:14
  • @conceptdeluxe - I placed 404.php at location D:\wamp\www, and it worked. But I have to keep that file at location D:\wamp\www\myWebsite. Is any setting required for this? –  Apr 10 '14 at 01:17
  • ErrorDocument 404 /myWebsite/404.php is working. Why the .htaccess file in myWebsite is looking for 404.php file in www folder instead of myWebsite folder? –  Apr 10 '14 at 01:18

3 Answers3

26

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.

conceptdeluxe
  • 3,753
  • 3
  • 25
  • 29
  • 1
    additional info: ie needs custom error page has > 512 bytes. details here https://perishablepress.com/important-note-for-your-custom-error-pages/ – Andre Chenier Jul 23 '19 at 08:46
22

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.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
  • 6
    ErrorDocument 404 404.php just prints 404.php on the browser. –  Apr 10 '14 at 01:07
  • 1
    As the first line of my questions says that I have .htaccess and 404.php both on root directory and it was also working for me some times ago. But to my surprise, I don't know why its not working today. I don't want to give absolute path. –  Apr 10 '14 at 01:12
6

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.

Chuck Le Butt
  • 47,570
  • 62
  • 203
  • 289
  • 1
    If an action begins with a `/`, the path to the file is not the full filepath on the server, but the filepath from the root of the domain/webserver. Example: a `404.php` file is located under `/var/www/domain.com/404.php`; in this case the path is `/404.php`, provided that the root of the website is `/var/www/domain.com` in vhosts file. – Binar Web May 03 '22 at 11:14