I'm having the following situation, I'm developing an API. I rewrite all traffic to one PHP script named: route.php
, using mod_rewrite
like:
1: RewriteEngine On
2: RewriteCond %{REQUEST_FILENAME} -f [OR]
3: RewriteCond %{REQUEST_FILENAME} -d
4: RewriteRule ^.* route.php [L]
Other files should not be accessible, that's why I am using a whitelist for accessing route.php
only. Therefor I use this:
order allow,deny
<FilesMatch "route\.php">
allow from all
</FilesMatch>
I would like to send all 1xx, 2xx (except 200), 4xx, and if possible 5xx HTTP status codes to a PHP script (let's say error.php?code=404
that shows the dynamic error page for that statuscode. In this case I probably have to allow access to error.php
also in the FilesMatch
part.
I found partly what I want, described in this article, but I can't implement or manage to get it working the way I described above.
My purpose is for the error.php
to show a JSON output (dynamically based on the statuscode) like {'statusCode':'404','status':'Not Found'}
including all common (security) HTTP headers I use.
This question didn't get much attention and I managed to answer the question myself. So hereby my answer. This is possible in the following way. Any improvement is welcome.
Below should be in the
.htaccess
file:The following should be in the
error.php
file:This doesn't seem to work for all HTTP error codes, like 414 "Request-URI Too Long" and always falls back to 403 Forbidden.