从这个URL
www.example.com/error_documents/404到此URL
www.example.com/404我尝试过许多不同的.htaccess规则,但都没有奏效。我只是试图隐藏URL中的/ error _pages/文件夹部分,而不进行任何实际重定向,因为如果我编写了一个正确的*RewriteRule,它只是不断地重复,我得到了一个ERR_TOO_MANY_REDIRECTS错误,因为如果您想转到一个未知的文件夹,错误文档将重定向到example.com/404,,如果我将它重写到example.com/404,它是一个未知的文件夹,所以它试图将我重定向到/error_documents/404页面,但是htaccess文件一直重定向到永久循环。
当前.htaccess文件
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+error_pages/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^error_pages/)^(.*)$ /error_pages/$1 [L,NC]这个.htaccess给了我永久的循环:
从www.example.com/unknownfolder到www.example.com/error_documents/404到www.example.com/404
这件事不断重复..。
我将cPanel用于ErrorDocuments,主要的.htaccess文件是:
ErrorDocument 400 http://example.com/error_pages/400
ErrorDocument 401 http://example.com/error_pages/401
ErrorDocument 403 http://example.com/error_pages/403
ErrorDocument 404 http://example.com/error_pages/404
ErrorDocument 503 http://example.com/error_pages/503
Options -Indexes
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]发布于 2021-10-29 00:24:15
ErrorDocument 400 http://example.com/error_pages/400 ErrorDocument 401 http://example.com/error_pages/401 ErrorDocument 403 http://example.com/error_pages/403 ErrorDocument 404 http://example.com/error_pages/404 ErrorDocument 503 http://example.com/error_pages/503
首先,您不应该在ErrorDocument指令中使用绝对URL --这就是导致外部(302)重定向和公开/error_pages位置和错误文档的原因。因此,这也会丢失有关导致错误的请求的信息。
但是,/400和/401等应该引用处理请求的实际文件。例如:/400.html和/401.html等。
您应该对错误文档使用根相对文件路径(从斜杠开始),然后Apache将发出内部子请求,而不是重定向。
例如:
ErrorDocument 400 /error_pages/400.html
ErrorDocument 401 /error_pages/401.html
ErrorDocument 403 /error_pages/403.html
ErrorDocument 404 /error_pages/404.html
ErrorDocument 503 /error_pages/503.html您的/error_pages现在仍然对最终用户完全隐藏。
不需要手动尝试从URL中删除它(因为它不应该出现在URL中)。如果需要,可以(可选)阻止对/error_pages目录的直接访问(注意不要阻止对错误文档的子请求)。
参考资料:
https://stackoverflow.com/questions/69762031
复制相似问题