Mobile app version of vmapp.org
Login or Join
Eichhorn148

: Htaccess ErrorDocument 500 not working I have a site whose .htaccess file contains: ErrorDocument 404 /errors/404.html ErrorDocument 500 /errors/500.html The 404 redirect works just fine, but when

@Eichhorn148

Posted in: #Htaccess #HttpCode500

I have a site whose .htaccess file contains:

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


The 404 redirect works just fine, but when I encounter a 500 Internal Server Error, it gives the default Internal Server Error message as opposed to /errors/500.html. It also says


Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.


More info: I can get it to throw 500 if I type a trailing slash on a .html page that has had the ".html" removed by the remaining part of the htaccess file (e.g. if I try to navigate to example.com/test/ where test is actually test.html, then I get an internal server error):
#example .com/page will display the contents of example.com/page.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ .html [L,QSA]
#302 from example.com/page.html to example.com/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /.*.html HTTP/
RewriteRule ^(.*).html$ / [R=302,L]

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Eichhorn148

2 Comments

Sorted by latest first Latest Oldest Best

 

@Jamie184

but when I encounter a 500 Internal Server Error, it gives the default Internal Server Error


The problem is that custom 500 error documents simply don't get triggered for errors in .htaccess - which is what's happening here. As Aakash has already quoted, this may come under the realm of a "malformed request". If you check your error log it should state: "core:error".

In fact it is a bit tricky to simulate a real error that will trigger the custom 500 error document.

However, you can manually trigger a 500 error, which will call your custom error handler with something like the following:

RewriteRule ^ - [R=500]



Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.


Admittedly, the wording of this is a bit confusing. However, all system generated 500 errors appear to contain this same text. I think it just means that it couldn't "handle the request"... there was no ErrorDocument to handle the request, so here's a catch-all 500 Internal Server Error. Which is all that a 500 error really is... an undefined error state, because nothing else fits (check your server error log for details).


More info: I can get it to throw 500 if I type a trailing slash on a .html page that has had the ".html" removed...


Not sure that this is really part of your question, but this 500 error is the result of a rewrite loop. Specifically:

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ .html [L,QSA]


Request: example.com/test/ (with the extra slash)

In this case the %{REQUEST_FILENAME} is /path/to/test (no trailing slash), so the condition %{REQUEST_FILENAME}.html -f is true (/path/to/test.html does exist).

However, the URL-path that is captured by the RewriteRule pattern does contain the trailing slash ie. test/ - it is not the same as the REQUEST_FILENAME in this instance. So the URL gets incorrectly rewritten to test/.html.

And the rewriting starts over again from the top (because the URL has changed)... test/.html.html, test/.html.html.html, etc. (Because the REQUEST_FILENAME is always /path/to/test.)

10% popularity Vote Up Vote Down


 

@Annie201

I would try two things -


Check permissions of 500.html (try setting it to 777 to be very sure - modify it later).
Try 500.htm (or 500.txt) instead of 500.html (just to be sure that your other rules in htaccess are not messing up with the ErrorDocument 500.html page). Also, remember to change the htaccess ErrorDocument rule to 500.htm (or 500.txt).


Also, as per httpd.apache.org/docs/2.4/mod/core.html#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.


Try some other ways to simulate 500 error.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme