如何在没有 403 错误的情况下将 .htaccess self-URL 重定向到站点索引?
How to redirect .htaccess self-URL to the site index without 403 error?
这是我的 .htaccess 文件:
Options -Indexes
AddDefaultCharset utf-8
ErrorDocument 403 /error.html
ErrorDocument 404 /error.html
RewriteEngine on
RewriteRule ^error.html$ / [R=301]
一切都按预期工作,如果我输入一个不存在的文件夹或文件,我将被重定向到站点索引。
访问 .htaccess 本身的问题,它是这样工作的:
mysite.com/existing_file.html -> returns file content
mysite.com/non_existing_file.html -> redirects to /
mysite.com/.htwhatever -> redirects to /
mysite.com/.htaccess -> 403 error
mysite.com/.htaccess_non_existent -> 403 error
似乎从.htaccess开始的任何东西returns 403错误
我试过添加
RewriteRule ^.htaccess$ / [R=301]
但它什么也没做。
为什么会这样,我该如何解决?
谢谢!
如果您想更改此行为,则必须先覆盖 <Files ".ht*">
,然后使用 RewriteRule
:
进行重定向
# allow requests for .ht* here first
<Files ".ht*">
Require all granted
</Files>
# redirect to / here
RewriteEngine On
RewriteRule ^\.ht / [L,R=301,NC]
这是我的 .htaccess 文件:
Options -Indexes
AddDefaultCharset utf-8
ErrorDocument 403 /error.html
ErrorDocument 404 /error.html
RewriteEngine on
RewriteRule ^error.html$ / [R=301]
一切都按预期工作,如果我输入一个不存在的文件夹或文件,我将被重定向到站点索引。
访问 .htaccess 本身的问题,它是这样工作的:
mysite.com/existing_file.html -> returns file content
mysite.com/non_existing_file.html -> redirects to /
mysite.com/.htwhatever -> redirects to /
mysite.com/.htaccess -> 403 error
mysite.com/.htaccess_non_existent -> 403 error
似乎从.htaccess开始的任何东西returns 403错误
我试过添加
RewriteRule ^.htaccess$ / [R=301]
但它什么也没做。
为什么会这样,我该如何解决?
谢谢!
如果您想更改此行为,则必须先覆盖 <Files ".ht*">
,然后使用 RewriteRule
:
# allow requests for .ht* here first
<Files ".ht*">
Require all granted
</Files>
# redirect to / here
RewriteEngine On
RewriteRule ^\.ht / [L,R=301,NC]