![](/img/trans.png)
[英]redirect everything after the root url to subfolder with .htaccess
[英].htaccess rewrite to show only root url and hide everything after /folder
从这个网址
www.example.com/error_documents/404
到这个网址
www.example.com/404
我尝试了许多不同的 .htaccess 规则,但没有一个奏效。 我试图从 URL 中隐藏/error_pages/文件夹部分而不进行任何实际重定向,因为如果我写了一个正确的 *RewriteRule,它只会不断重复自己,我会收到 ERR_TOO_MANY_REDIRECTS 错误,因为如果你想转到一个未知的文件夹,错误文档重定向到example.com/error_documents/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]
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
和错误文档的位置的/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
目录(注意不要阻止对错误文档的子请求)。
参考:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.