Create custom PHP error pages in cPanel without redirecting them away from the page that has thrown the error
Asked Answered
B

2

10

I am trying to make my web server have customized error documents/pages but with PHP code included in them, without redirecting them away from the page that has thrown the error.

Let's say we are navigating to http://website.com/pages/1 and it was to throw the error 500, by default the page would just be a blank white page with the text "Error 500 (Internal Server Error)" which would look something like:

https://static.mcmap.net/file/mcmap/ZG-AbGLDKwfpKnMxcF_AZVLQamyA/GuGIP.png

As you can see from the above, it has NOT redirected away from the page that has thrown the error. I want this page to look "a part of the website" but with PHP content included in them.

I cannot include PHP content in the error pages over in the cPanel by editing the pages you see below:

https://static.mcmap.net/file/mcmap/ZG-AbGLDKwfpKnMxcF_AZVLQamyA/ZWKzQ.png

If I was to edit the error 500 page above, with the content below the problem would be that http://website.com/pages/1 would redirect to http://website.com/500.shtml and then in turn redirect to http://website.com/500.php

I do NOT want it to redirect at all otherwise it means refreshing the page essentially will just refresh the 500.php page rather than refreshing the /pages/1

<script language="javascript">
    window.location.href = "http://www.website.com/500.php"
</script>
<meta http-equiv="refresh" content="0;URL='http://website.com/500.php'" />  

The same exact problem would exist, just without it being 2 chain redirects instead it would just be 1 redirect using the following in the .htaccess file

ErrorDocument 400 http://website.com/400.php
ErrorDocument 401 http://website.com/401.php
ErrorDocument 403 http://website.com/403.php
ErrorDocument 404 http://website.com/404.php
ErrorDocument 500 http://website.com/500.php

Current Result: Redirecting to /500.php

Expected Result: Displaying 500.php in/on http://website.com/pages/1 without redirect

How can I have custom error pages WITH php content WITHOUT making it redirect away from the page that has thrown the error?

Is there a way to do it via root (could I get my hosts to do something, if so what?)

Bookcraft answered 16/9, 2015 at 4:45 Comment(2)
@Kuya how are you even planning on using a error handler for a internal server error, error handlers don't even always get called properly if a internal server error occurs. And I've already said it but I'll say it again, generating a full page instead of simply using htaccess is foolishPotaufeu
@Kuya You don't pick bits out of a post and answer to those parts, you answer to the question which is in bold and has a question mark at the end of it: How can I have custom error pages WITH php content WITHOUT making it redirect away from the page that has thrown the error?. My post is CLEARLY on about redirecting and preventing the redirectionBookcraft
P
3

As the others have answered this should work:

ErrorDocument 500 /500.php

If you do not specify a full URL it just displays the page (an internal redirect), if you do specify the full URL, then it issues a redirect back to ths client. This is as described by Apache here: http://httpd.apache.org/docs/2.4/mod/core.html#errordocument

Note that when you specify an ErrorDocument that points to a remote URL (ie. anything with a method such as http in front of it), Apache HTTP Server will send a redirect to the client to tell it where to find the document, even if the document ends up being on the same server.

The fact that the above is not working for you means one of several things:

  1. You are doing it wrong and still specifying the full URL even though you claim not to be.
  2. Cpanel is adding the full URL (not sure how you edit the .htaccess file or if it's done via above GUI).
  3. Your .htaccess file is being ignored (perhaps because you are setting the values in the GUI or perhaps because FileInfo is not set at server level so this cannot be specified in .htaccess file (see here: http://httpd.apache.org/docs/2.4/custom-error.html), in which case it must be picking up the full URL itself from other config somewhere.
  4. The 500.php script is doing the redirecting itself (should be obvious from developer tools if preserve log and see if one page is returned or two).
  5. There is a bug in Apache (that I and others are not aware of).
  6. Magic

Can you check out those things?

Might also help to know what version of Apache you are on?

Also have you tried a plain 500.html file just to see if this works without redirecting? In theory a 500.php file should be fine too but would be interesting to narrow it down (and would also show if that change to 500.html was picked up and used or if it was still pointing to 500.php, which would perhaps show this was being picked up from other config than you think).

Portwine answered 22/9, 2015 at 17:15 Comment(0)
P
0

If you add something like this in your .htaccess file it should simply display the error page without a redirect. I've never used absolute paths like you're doing in your example for error pages so I'm not sure what kind of behavior it causes

ErrorDocument 404 /404.php 
Potaufeu answered 18/9, 2015 at 8:31 Comment(1)
ErrorDocument 500 /500.php redirects to website.com/500.phpBookcraft

© 2022 - 2024 — McMap. All rights reserved.