我有一个域,我试图为错误页面设置自定义redirect。
我的.htaccess文件看起来像这样:
RewriteEngine On AddDefaultCharset UTF-8 DefaultLanguage en-US # disable TRACK and TRACE http methods. 'RewriteEngine On' is required! RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK|OPTIONS|HEAD|PUT|DELETE) RewriteRule .* - [F] Options All -Indexes ServerSignature Off <ifModule mod_headers.c> Header unset X-Powered-By </ifModule> <Files .htaccess> order allow,deny deny from all </Files> <IfModule php5_module> php_value session.cookie_httponly true #php_value session.cookie_secure true </IfModule> RewriteCond %{QUERY_STRING} _escaped_fragment_=(.*) #to block all links to '.gif', '.jpg','.js' and '.css' files which are not from the domain name 'http://www.example.com/' RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !^http://(www\.)?example.com/.*$ [NC] RewriteRule \.(gif|jpg|css|js)$ - [F] #to deny requests containing invalid characters RewriteBase / RewriteCond %{THE_REQUEST} !^[AZ]{3,9}\ [a-zA-Z0-9\.\+_/\-\?\=\&]+\ HTTP/ [NC] RewriteRule .* - [F,NS,L] # Secure directories by disabling execution of scripts AddHandler .php .pl .py .jsp .asp .htm .shtml .sh .cgi Options -ExecCGI #Error page handeller ErrorDocument 400 /htaccessSample/errordocs/error-code.php ErrorDocument 401 /htaccessSample/errordocs/error-code.php ErrorDocument 403 /htaccessSample/errordocs/error-code.php ErrorDocument 404 /htaccessSample/errordocs/error-code.php
当我在URL上键入如下内容时:
example.com/aboutUs :一切正常。 example.com/xxygsds被redirect到自定义404错误页面。 example.com/<> :这将redirect到403禁止页面。 但是,redirect到apache/error/HTTP_FORBIDDEN.html.var页面,而不是上面提到的自定义页面。 我的问题:
1.为什么这个redirect发生?
2.如何在所有情况下redirect到自定义403错误页面?
我不知道为什么这样做。 与我的403页面有同样的问题。 如果你想redirect一个特定的403页面,使用这个:
<Files some_thing> order Deny,Allow Deny from all </Files>
尝试将错误文档位置移动到.htaccess文件的顶部。 我看了一下你的例子,试图弄清楚发生了什么事情,然后终于意识到,在这种情况下,被告知要退出之前退出的东西。
Apache正在按顺序读取指令,并退出L个案例。 L隐含在F:
当使用[F]时,隐含一个[L] – 也就是说,响应立即返回,并且没有进一步的规则被评估。 https://httpd.apache.org/docs/2.4/rewrite/flags.html
所以我猜测apache根本就不会在你的<>例子的真实,匹配的情况下得到这套指令。 然后将页面发送到默认的apache 403错误页面,因为它在到达这些声明之前退出。 你的404工作,因为它从来没有触发任何包含L条件的规则,即最后一件事要执行,所以达到了htaccess规则的底部。
我从来没有遇到这种情况,因为我总是把默认的错误页面放在指令的TOP上,习惯上还有其他一些核心的东西,比如php ini的调整等等。
重写在底部。
目标是,所有你想告诉apache的信息都会在apache被重写的时候告诉apache,或者htacess / config规则的结尾。 如果不是htaccess,apache会在开始为你的网站提供服务的时候知道所有的规则,但是使用htacess,我相信它只是读取它们,并且执行文件告诉它的内容。
我相信这是对的,但我不是100%肯定的,因为我总是把那些应该在顶端的东西,从而没有触发你的情况。 关键是要认识到L,最后,真的意味着Last,然后要知道F意味着L.
一般来说,在处理apacheconfiguration时,应该组织起来:
# top generics etc #Error page handler ErrorDocument 400 /htaccessSample/errordocs/error-code.php ErrorDocument 401 /htaccessSample/errordocs/error-code.php ErrorDocument 403 /htaccessSample/errordocs/error-code.php ErrorDocument 404 /htaccessSample/errordocs/error-code.php AddDefaultCharset UTF-8 DefaultLanguage en-US # Disable Directory Browsing, not related to rewrite Options All -Indexes # Secure directories by disabling execution of scripts AddHandler .php .pl .py .jsp .asp .htm .shtml .sh .cgi Options -ExecCGI ServerSignature Off <ifModule mod_headers.c> Header unset X-Powered-By </ifModule> <IfModule php5_module> php_value session.cookie_httponly true #php_value session.cookie_secure true </IfModule> <Files .htaccess> order allow,deny deny from all </Files> ## NOW do the set of rewrite rules, everything that apache needs to ## know has been told to it by this point RewriteEngine On # disable TRACK and TRACE http methods. 'RewriteEngine On' is required! RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK|OPTIONS|HEAD|PUT|DELETE) RewriteRule .* - [F] RewriteCond %{QUERY_STRING} _escaped_fragment_=(.*) #to block all links to '.gif', '.jpg','.js' and '.css' files which are not from the domain name 'http://www.example.com/' RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !^http://(www\.)?example.com/.*$ [NC] RewriteRule \.(gif|jpg|css|js)$ - [F] #to deny requests containing invalid characters RewriteBase / RewriteCond %{THE_REQUEST} !^[AZ]{3,9}\ [a-zA-Z0-9\.\+_/\-\?\=\&]+\ HTTP/ [NC] RewriteRule .* - [F,NS,L]
请记住在.htaccess文件的结尾总是添加换行符/换行符。 你的版本是非常随机的和混乱的,这是我认为你的问题的实际原因,如果你只是习惯于清晰和一致地订购你的规则(即重写不与其他规则混合),你会发现它一般来说,使用apache更容易。