Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I redirect an Error 403 to root in .htaccess

I am not very firm in Apache, so please excuse if the question might seem a bit obvious. I would like to redirect requests that would result in an Error 403 in specific directories to my webservers root by means of a .htaccess file. so https://thedomain.com/secretlair/ -> https://thedomain.com

so far i used:

RewriteEngine on
RewriteBase /
RewriteRule (.*) https://thedomain.com/$1 [R=301,L]

which works but creates the problem of also redirecting https://thedomain.com/secretlair/thefile.txt to https://thedomain.com/thefile.txt

what do I need to change to make this work?

Thanks a lot!

like image 561
zantafio Avatar asked Dec 05 '25 00:12

zantafio


1 Answers

You can just use a ErrorDocument handler for 403 error instead of a rewrite rule like this:

ErrorDocument 403 https://thedomain.com/

Make sure to clear your browser cache before testing this change.

like image 199
anubhava Avatar answered Dec 07 '25 16:12

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!