On my website I would like it to redirect all error 500 errors to /500.html. As of current some of the page displays with GET
http://localhost/ [HTTP/1.0 500 Internal Server Error 1ms]
in the Developer Tools. I have tried implementing this by editing /etc/apache2/sites-enabled/000-default.conf
. The file looks like this:
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
ErrorDocument 500 /500.html
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
I have also tried editing apache2.conf
and .htaccess
but I get the same result.
P.S: I have tried restarting Apache every time!
If you are trying to intercept 500 errors from PHP in apache using mod_php
with SetHandler
(A very common way), then the answer is that you can't. To the best of my knowledge it's not a supported feature.
However there are ways such as using PHP via it's FastCGI interface (PHP-FPM) and the Apache proxy module to achieve what you require.
First, you may need to ensure that mod_proxy
and proxy_fcgi_module
are installed on your server and enabled in the Apache config like so:
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
Also ensure that PHP-FPM is installed and running as a service, the default port it uses is 9000.
Then replace your apache PHP configuration (possibly in /etc/httpd/conf.d/php.conf) with the following.
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/var/www/html/$1
DirectoryIndex /index.php index.php
This will mean that you are now using the Apache Proxy module to send traffic to PHP. It is possible to instruct Apache to intercept error responses from the proxy module and use custom error handlers with the following in your VirtualHost
section.
ProxyErrorOverride On
ErrorDocument 500 /500.html
Custom error documents are configured using the ErrorDocument
directive, which may be used in global, virtualhost, or directory context. It may be used in .htaccess
files if AllowOverride
is set to FileInfo
.
ErrorDocument 500 "Sorry, our script crashed. Oh dear"
ErrorDocument 500 /errors/not_found.html
ErrorDocument 500 http://error.example.com/server_error.html
The syntax of the ErrorDocument directive is:
ErrorDocument <3-digit-code> <action>
where the action will be treated as:
Apache documentation
Keep in mind that the Apache looks for the 404 page located within the site's server root. Meaning that if you place the new error page in a deeper subdirectory, you need to include that in the line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With