Search code examples
apache.htaccessmod-rewrite

Redirect based on rules and error type htaccess


I have been trying to piece together my .htaccess file, I have not found a lot of information about what I am trying to accomplish or if I am over thinking it.

I am using the following to disable directory browsing it works but I would like to redirect it to a custom 404 error page I have created.

# Disable Directory Browsing
Options All -Indexes

Deny access to directories or files I am using the following

####
# Deny access to certain directories that SHOULD NOT be exposed.
####
RewriteRule ^error/ - [L,R=403]
RewriteRule ^assets/ - [L,R=403]
RewriteRule ^plugins/ - [L,R=403]
RewriteRule ^libraries/ - [L,R=403]
RewriteRule ^includes/ - [L,R=403]
RewriteRule ^bootstrap.php - [L,R=404]

all of these work as it should but I want to redirect to a custom error page in my script folder

example.com/errors/

All i have found is this

ErrorDocument 404 /errors/error-404.html

but have been unsuccessful in getting any of the errors redirected to the one I created.


Solution

  • Without seeing the actual response you are seeing or your full .htaccess file, my guess would be that you are blocking access to the /errors directory that contains your custom error documents. The error documents themselves need to be publicly accessible (although there are tricks you can employ to block access and serve an appropriate HTTP status code).

    If you block access to the error document then Apache will fallback to the default Apache response with an additional message along the lines of:

    Additionally, a "4xx/5xx error" error was encountered while trying to use an ErrorDocument to handle the request.