0

How can I show a custom page when a 500 Internal Server Error occurs?

I created a page called '500.php'.
Then added the following line to my '.htaccess' file to handle the error:

ErrorDocument 500 /500.php

And the following line to test/trigger a 500 error:

e

But the default 500 page shows up - not my custom one.

I tried editing the first line to this (whole URL):

ErrorDocument 500 http://danispringer.com/500.php

But then I get the "Too many redirects" error.

How can I do this?

  • You don't need to use a RewriteRule if you're going to use ErrorDocument. – Funk Forty Niner Oct 18 '17 at 00:37
  • `ErrorDocument 500 /500.php` is enough. – Funk Forty Niner Oct 18 '17 at 00:40
  • there's no need to "trigger" an error per se. If and when a script errors out as a server error. Apache takes over and looks for the ErrorDocument 500 automatically. – Funk Forty Niner Oct 18 '17 at 00:44
  • that is exactly it ;-) the only way to test it would be with a conditional statement of something that doesn't work, and with something that does. All Apache directives that can be caught, will be triggered and handled respectively of the handle request. – Funk Forty Niner Oct 18 '17 at 00:48
  • 1
    set your server settings to not show errors but log them and then deliberately leave out a missing semi-colon somewhere; that will trigger a server error. Best I can add to all this and we've just triggered the *"Please avoid extended discussions in comments. Would you like to automatically move this discussion to chat?"*. The question could get closed and/or trigger a "too chatty, moved to chat...." by a mod. You'll need to let all that sink in and try something ;-) cheers. I believe I answered it somewhat. – Funk Forty Niner Oct 18 '17 at 00:53
  • welcome but that answer below, is basically what I wrote up here. I figured you'd of invited me to move my comments to the answers area. I can't delete my comments, I feel I answered the question but if you think I didn't, then you need to tell me. At this point, I don't know what you want. – Funk Forty Niner Oct 18 '17 at 01:23

1 Answers1

0

First of all I would not use a php file if not absolutely necessary, because if you have a problem with php that you also can't show your 500.php page.

I would create a folder /errors and put all my error files in this e.g.:

/errors/404.html
/errors/403.html
/errors/500.html

Now If you looking for a solution to test a 500 error then just create a folder (e.g. test500folder) and put an invalid .htaccess file in this folder /test500folder/.htaccess:

abc

Now you can test you 500 error if you open example.com/test500folder/

but remove: RewriteRule ^ - [R=500] and everything else but use only

ErrorDocument 403 /errors/403.html
ErrorDocument 404 /errors/404.html
ErrorDocument 500 /errors/500.html

in your main htaccess file to test.

Edit:

So I assume that you, or your provider, have configured a location for all you ErrorDocumentes, but these files are deleted now. You have to replace exact this files or configure some other in your providers back end, if possible.

Andhi Irawan
  • 456
  • 8
  • 15
Webdesigner
  • 1,954
  • 1
  • 9
  • 16
  • I added two lines to the empty htaccess I made for the 500folder/. One line has `e` to trigger an error, the second has `ErrorDocument 500 /folder500/500.html` . Makes sense? What URL should I visit? –  Oct 18 '17 at 01:11
  • Please see updated GitHub: https://github.com/DaniSpringer/website (unfortunately I still see the default error page when visiting /folder500) –  Oct 18 '17 at 01:22
  • It is! I named it without a dot only in GitHub so you can see it. –  Oct 18 '17 at 01:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/156962/discussion-between-webdesigner-and-dani). – Webdesigner Oct 18 '17 at 01:25
  • A .htaccess will only apply to one site, and not the entire server. Just as you don't want to use php file to report server errors, neither do you want a single site responsible for reporting server errors, since the problem may be with the site's configuration. – Cerin Sep 10 '19 at 17:49