将 mod_rewrite 与 ErrorDocument 结合使用
我在 PHP 框架中使用 mod_rewrite 将用户友好的链接重定向到应用程序的一个主要入口点。我正在使用的 .htaccess 文件如下所示:
<IfModule mod_rewrite.c>
Options FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
现在,我想将该规则与 ErrorDocument
指令一起使用。我该怎么做?当缺少控制器时,我的框架将响应代码设置为 404
:
/**
* Sets the HTTP response code.
*
* @param int $errorCode The response code to return. <b>Supported codes: </b>301, 307, 400, 401, 403, 404, 500,
* 501, 502, 503
*/
function setHTTPStatus($errorCode) {
$httpStatusCodes = array(301 => 'Moved Permanently', 307 => 'Temporary Redirect', 400 => 'Bad Request',
401 => 'Unauthorized', 403 => 'Forbidden', 404 => 'Not Found', 500 => 'Internal Server Error',
501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable');
if (array_key_exists($errorCode, $httpStatusCodes)) {
header("HTTP/1.0 $errorCode $httpStatusCodes[$errorCode]", true, $errorCode);
exit;
}
}
我尝试添加行 ErrorDocument 404 /index.php?url=404
和 ErrorDocument 404 / error
但似乎没有一个起作用,我得到了默认浏览器的错误页面。
怎么了?
谢谢。
I am using mod_rewrite in my PHP framework to redirect user-friendly links to one main entry point of the application. The .htaccess file I am using looks like that:
<IfModule mod_rewrite.c>
Options FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
Now, I would like to use that rule with the ErrorDocument
directive. How should I do this? My framework sets the response code to 404
when there's a controller missing:
/**
* Sets the HTTP response code.
*
* @param int $errorCode The response code to return. <b>Supported codes: </b>301, 307, 400, 401, 403, 404, 500,
* 501, 502, 503
*/
function setHTTPStatus($errorCode) {
$httpStatusCodes = array(301 => 'Moved Permanently', 307 => 'Temporary Redirect', 400 => 'Bad Request',
401 => 'Unauthorized', 403 => 'Forbidden', 404 => 'Not Found', 500 => 'Internal Server Error',
501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable');
if (array_key_exists($errorCode, $httpStatusCodes)) {
header("HTTP/1.0 $errorCode $httpStatusCodes[$errorCode]", true, $errorCode);
exit;
}
}
I tried adding the line ErrorDocument 404 /index.php?url=404
and ErrorDocument 404 /error
but none seem to have worked and I get the default browser's error page.
What is wrong?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有简单的方法可以通过 .htaccess 文件来做到这一点。一旦您进入 php 代码区域,apache 就会处理 .htaccess 文件和 httpd.conf 文件,并移交到框架前端
控制器——在本例中是框架的index.php 文件。
你使用什么框架?
处理这个问题的最佳方法是使用框架错误处理,而不是内置的 apache 处理。 apache 404 对于任何非框架服务的页面(如图像、css、js 等)都适用。您的框架应该在某处有一个配置文件来设置默认错误视图。
There is no easy way to do this via the .htaccess file. Once you're in php code land, apache has processed through the .htaccess file and httpd.conf files and made a handoff to the frameworks front
controller--in this case the index.php file for the framework.
What framework are you using?
The best way to handle this is to use the frameworks error handling, not the baked-in apache handling. The apache 404 is fine for any non-framework served pages like an image, css, js, etc... Your framework should have a configuration file somewhere to set the default error view.