Apache ErrorDocument for error 500 not working - apache

I know this might be a duplicate, but I have better details.
I have the .htaccess file set to display a custom error page for the most common errors:
# remove .php
RewriteEngine On
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]
#Add custom error pages
ErrorDocument 404 /errors/404
ErrorDocument 403 /errors/403
ErrorDocument 500 /errors/500
When I purposely trigger an error 500 it just gives me the default Apache page for that and also says: Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
I can access the error page by typing localhost/errors/500 so it's not a problem with the permissions. I searched many forums but haven't found a solution.
Edit: The error 500 was triggered from a malformed request by the rewrite engine. If you type example.com/index/ it tried to go to /index/.php and that caused error 500.

A 500 Internal Server Error is a very general error. It indicates that the server encountered a serious error and is simply unable to process the request as normal.
Many of these 500 errors simply cannot be caught with a custom error document defined "late" in .htaccess.
You stand a better chance if the ErrorDocument is defined in the server config. But even that will not be soon enough for some errors.
However, having said that...
...and also says: Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request. I can access the error page by typing localhost/errors/500 so its not with the permissions.
Edit: The error 500 was triggered from a malformed request by the rewrite engine. If you type example.com/index/ it tried to go to /index/.php and that caused error 500
It does appear to be trying to serve a custom (500) error document, but this is also encountering a 500 error.
Specifically, requesting /index/ results in a rewrite loop, ie. /index/.php to /index/.php.php to /index/.php.php.php etc. It's when the server reaches the limit for internal redirects (default 10) does it trigger a 500 error.
Yes, I see this as well for rewrite loops. Even though the error does not appear to interfere with the serving of the ErrorDocument; the message seems to suggest that it does. Requesting the error document directly is OK.
I wonder (purely hypothetical)... since the "internal redirects" limit has been reached in this request (because of the rewrite loop), the ErrorDocument (that is also served by an internal redirect) can't be served because this limit has already been reached!? So this then triggers a 500 error for the same reason! Strictly speaking, the ErrorDocument is served via an internal subrequest (not an internal redirect), although the Apache documentation for the LimitInternalRecursion directive seems to group "internal redirects" and "subrequests" together.
I can't just think of another way of triggering a 500 error, other than triggering it in code (as below - which works OK). Any (fatal) parse errors in the code trigger a 500 error on every request.
RewriteRule ^foo$ - [R=500]
The above should serve your custom 500 ErrorDocument as normal, the ErrorDocument itself is OK.

Related

Redirect 4xx to a custom page with 200 response code in httpd.conf

I am trying to get any 403 request to go to a custom error page with 200 OK request (for security reason).
Tried this to redirect any 4xx error code to a custom error page
ErrorDocument 403 /shared/error.html
But this will not change the response code (it will still be 404).
There this option but it will give 302 here
Tried this to change the error code:
RewriteEngine on
RewriteRule ^/shared/.*/$ /shared/error.html [R=200,L]
But somehow it doesn't redirect to the custom error page I want to.
Tried it with absolute path as well:
RewriteEngine on
RewriteRule ^/shared/.*/$ https://%{SERVER_NAME}/shared/error.html [R=200,L]
Still doesn't work. Is it not possible or am I missing something obvious? This is my first time modifying but I did some research already. Any help is appreciated.
If you can use ErrorDocument for the code 200:
ErrorDocument 200 /shared/error.html
RewriteEngine on
RewriteRule ^/shared/.*/$ anything [R=200]
When the rule is applied it will execute the redirection to the ErrorDocument straight away, and that is the reason why you can put "anything" in there.
Check the documentation for the R flag (https://httpd.apache.org/docs/current/rewrite/flags.html):
The status code specified need not necessarily be a redirect (3xx) status code. However, if a status code is outside the redirect range (300-399) then the substitution string is dropped entirely, and rewriting is stopped as if the L were used.
Beware that you might not want this redirection for the whole code, so you should apply it within a context. And take cara with your regex so you don't fall in a loop.
Source: https://www.askapache.com/htaccess/crazy-advanced-mod_rewrite-tutorial/
If you'd use a php-script for example instead of your static html-document, you can override the response code inside your script.
See: http://php.net/manual/en/function.http-response-code.php

Redirect to external site on 500 error with .htaccess

I am working on a website (scratchyone.com). I need to have my .htaccess redirect to an external page
http://scratchywon.github.io/scratchyone.com/errors/500.html
on a 500 error. I am forcing a 500 with php.
http://scratchyone.com/500/
Here is my current code:
ErrorDocument 500 http://scratchywon.github.io/scratchyone.com/errors/500.html
EDIT: ErrorDocument 404 http://scratchywon.github.io/scratchyone.com/errors/500.html works
Now that I am using php to send the 500 error, the page doesn't display. It just displays the browser's default "500: Could not display"
Internal error in .htaccess not relay custom ErrorDocument (!) and simulate 500 error via PHP is not quite possible - when your script run it's too late - error document redirection is handled by apache mod_core (!) and PHP only send status.
So I think you can forget about PHP 500-simulation.
Try to make a directory next to main .htaccess and inside make faulty .htaccess
Or maybe this will be helpful for you:
https://www.digitalocean.com/community/tutorials/how-to-configure-apache-to-use-custom-error-pages-on-ubuntu-14-04
Section about 500 provide information how to simulate this error via bad proxy.

Can't redirect to a maintenance page when serveur return 500 Internal server error

Ive made this .htaccess file
ErrorDocument 404 /404.php
ErrorDocument 500 /maintenance.php
But when i try to access to http://www.mupiz.com/admin/TESTS.php (there's a parse error in it)
The serveur does not redirect to maintenance.php
Any ideas ?
Thanks
You have some confusion about custom error handling.
500 is for Internal Server Error in Apache before it invokes PHP module. Once PHP module is invoked it is up to the PHP to handle everything. Parse error anyway is not a 500 and even if you make PHP cause 500, Apache's 500 handler won't be invoked.

Apache Error directive ignoring ErrorDocument directive

I wrote on my .htaccess an if to check if the *mod_rewrite* is installed and throw error 500 if not.
ErrorDocument 500 /500.html
<IfModule !mod_rewrite.c>
Error "mod_rewrite not installed"
</IfModule>
If i get an 500 error by it will show the 500.html. But if it gets that same error from the Error directive, it will show the default error page instead. Why ? How do i fix it ?
Well, after thinking a lot about it and reading a bit, i believe that the Error function simply makes the .htaccess parsing to be cancelled - and the error to be logged - and then throw an 500 error from the apache config that has been parsed before this one (for example, an .htaccess from another folder over this one or the httpd.conf from apache)

How to get apache to load custom page for 414 error

Using htaccess, I've managed to make error pages show up beautifully on my site like such:
ErrorDocument errorcode /pathtopage
The included code works for every single error code I've tested except for Error 414 Request URI too long. It seems that Apache is completely ignoring the htaccess file in this case - is there a way to get it to work?
Some error pages like 414 and 400 are only customizable in the server configuration. I don't know the reason for that, but if you have an own server that should be no problem.
From ErrorDocument
Although most error messages can be overridden, there are certain circumstances where the internal messages are used regardless of the setting of ErrorDocument. In particular, if a malformed request is detected, normal request processing will be immediately halted and the internal error message returned. This is necessary to guard against security problems caused by bad requests.
It seems, that a "414 Request-URI Too Large" is considered severe enough, to ignore an appropriate ErrorDocument 414 setting.
For a "dirty hack" you can use something I use sometimes to my clients
ErrorDocument 414 '<meta http-equiv="refresh" content="0;URL=/error/414"><script>document.location.href=/error/414;</script>'
It works fine in most situations. Also consider the alternative of RewriteCond, something like this.. Is only a pseudo example.. (for regex examples you must pay, just joking)
RewriteCond %{REQUEST_URI} !^({max leght of uri here})$
RewriteRule ^(.*)$ /error/414
Hope to give you an idea, cya