如何将错误 403 重定向到 .htaccess 中的根目录

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

我对 Apache 不是很坚定,所以如果问题看起来有点明显,请原谅。 我想通过 .htaccess 文件将在特定目录中导致错误 403 的请求重定向到我的网络服务器根目录。 所以https://thedomain.com/secretlair/ -> https://thedomain.com

到目前为止我使用了:

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

这有效,但也会产生重定向问题 https://thedomain.com/secretlair/thefile.txt to https://thedomain.com/thefile.txt

我需要更改什么才能使这项工作正常进行?

非常感谢!

您可以只对 403 错误使用 ErrorDocument 处理程序,而不是像这样的重写规则:

ErrorDocument 403 https://thedomain.com/

确保在测试此更改之前清除浏览器缓存。