简体   繁体   English

.htaccess 重写 ErrorDocument 没有被重定向

[英].htaccess rewrite ErrorDocument not being redirected

I removed the php directory from my URL and remove the .php extension.我从我的 URL 中删除了php目录并删除了 .php 扩展名。 But now I am trying to add my 404/500 error to make my website look nice.但现在我试图添加我的 404/500 错误以使我的网站看起来不错。

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /php/$1 [QSA,L]

ErrorDocument 404 /errors/404.php
ErrorDocument 500 /errors/500.php

When I redirect the errorDocument to an external page it works but not to my local file.当我将 errorDocument 重定向到外部页面时,它可以工作,但不能重定向到我的本地文件。 I get a 500 Internal Server error .我收到500 Internal Server error I could make a workaround and just make it go to an external page what will be my page.我可以做一个解决方法,让它转到一个外部页面,这将是我的页面。 But that's not really clean.但这并不是很干净。 Is anyone able to help me with this?有人能帮我解决这个问题吗?

I think you must match against DOCUMENT_ROOT as well as add this RewriteCond %{REQUEST_FILENAME}\\.php -f before adding .php like this :我认为您必须与DOCUMENT_ROOT匹配,并在添加.php之前添加此RewriteCond %{REQUEST_FILENAME}\\.php -f ,如下所示:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/php%{REQUEST_URI}\.php -f
# or you could do it like this 
# RewriteCond %{DOCUMENT_ROOT}/php%{REQUEST_URI} -f
# without php according to your setting
RewriteRule ^(.*)$ /php/$1 [QSA,L]


ErrorDocument 404 /errors/404.php
ErrorDocument 500 /errors/500.php

because whenever you send wrong request to any target destination you must make sure that target will handle it and there are enough conditions to classify which one should go , otherwise server will generate error.因为每当您向任何目标目的地发送错误的请求时,您必须确保目标会处理它并且有足够的条件来分类应该去哪个,否则服务器会产生错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM