使用 php bootstrap 时处理错误文档的正确方法

发布于 2024-10-08 12:58:59 字数 1105 浏览 0 评论 0原文

我正在使用 php bootstrap 设置一个网站。看来 ErrorDocument 指令优先于 RewriteRule 指令被放弃。不过没关系,标头仍然正确发送,所以我想我必须在 php 中监视它们并手动重定向到正确的错误文档。但问题是,在我向自定义页面发送一些特定错误之前,其余的都留下了默认的 apache 错误消息,现在,我可以看到如何重定向我的特定页面,但我丢失了所有默认消息。我无法添加对所有可能错误的支持。而且,我还没有尝试过,但我想这也会扩展到重定向状态代码,这是我需要的,因为该站点正在替换同一服务器上的旧站点。

正确的做法是什么?我希望我只是在我的 .htaccess 中做错了什么,其主要部分如下:

# .htaccess

# redirect error documents - commented since they don't work anyway
# ----------------------------------------------------------------------
#ErrorDocument 404 /error/404/
#ErrorDocument 403 /error/403/
#ErrorDocument 401 /error/401/
#ErrorDocument 500 /error/500/

# Redirect to bootstrap
# ----------------------------------------------------------------------
RewriteEngine on
RewriteBase /
RewriteRule !\.(ico|jpg|png|svg|js|css)$ index.php

# Prevent hotlinking 
# ----------------------------------------------------------------------
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?.*\.mysite\.net/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule \.(ico|jpg|png|svg|js|css)$ - [F]

感谢您的帮助。

I'm setting up a site using a php bootstrap. It seems the ErrorDocument directives are given up in preference for the RewriteRule's. That's ok though, the headers are still sent correctly, so I guess I have to monitor them in the php and redirect to the correct error document manually. The problem though is that while before I was sending a few specific errors to custom pages and the rest were left with the default apache error message, now, I can see how I can redirect my specific pages, but I lose all the default messages. I can't add support for all possible errors. And, I haven't tried it, but I guess this will extend to redirect status codes too, something that I will require since this site is replacing an old one on the same server.

What is the correct way to go about this? I'm hoping I'm just doing something wrong in my .htaccess, the salient parts of which follow:

# .htaccess

# redirect error documents - commented since they don't work anyway
# ----------------------------------------------------------------------
#ErrorDocument 404 /error/404/
#ErrorDocument 403 /error/403/
#ErrorDocument 401 /error/401/
#ErrorDocument 500 /error/500/

# Redirect to bootstrap
# ----------------------------------------------------------------------
RewriteEngine on
RewriteBase /
RewriteRule !\.(ico|jpg|png|svg|js|css)$ index.php

# Prevent hotlinking 
# ----------------------------------------------------------------------
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?.*\.mysite\.net/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule \.(ico|jpg|png|svg|js|css)$ - [F]

Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

jJeQQOZ5 2024-10-15 12:58:59

好问题,我们通过 PHP 来解决。

我们使用我们构建的专有框架。我们有一个包含所有有效 url 或“路由”的表(很像代码igniter 或kohana 中的)。我们做两件事。

设置 htaccess,以便如果物理服务器上存在文件,则显示该文件。所以 htaccess 看起来像这样:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?r=$1 [L,QSA] 

解释 - 重写任何 url,例如 www.domain.com/myurl 以转到 index.php?r=myurl,除非 url 是文件 (! -f) 或服务器上存在的目录 (!-d)。

然后我们让引导文件index.php 执行如下操作:

$route = $_GET['r'];
db::where('route', $route);
$result = db::get('routes')->fetch();
if(!$result) show_error('404');

然后我们将show_error('404') 定义为

function show_error($type){
switch($type){
  case '404':
  default:
    header("HTTP/1.0 404 Not Found");
    include('404.html');
    exit;
    break;
 }
}

使用此技术,大多数其他错误(例如403 无法访问等)仍将由apache 提供服务。因为例如,如果存在受密码保护的目录,则重写将导致服务器显示该目录,然后服务器将要求输入密码,如果未提供有效密码,则会出现 403。

祝你好运!

Good question, we work it through PHP.

We use a proprietary framework that we built. We have a table that contains all the valid urls, or "Routes" (much like in code igniter or kohana). We do two things.

Setup the htaccess so that if a file exists on the physical server, then show that file. so the htaccess looks something like this:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?r=$1 [L,QSA] 

explanation - rewrite any url, such as www.domain.com/myurl to go to index.php?r=myurl, except when the url is a file (!-f) or a directory (!-d) that exists on the server.

Then we have our bootstrap file, index.php do something like this:

$route = $_GET['r'];
db::where('route', $route);
$result = db::get('routes')->fetch();
if(!$result) show_error('404');

and then we have defined show_error('404') as

function show_error($type){
switch($type){
  case '404':
  default:
    header("HTTP/1.0 404 Not Found");
    include('404.html');
    exit;
    break;
 }
}

Using this technique, most of the other errors, like 403 no access etc. will still be served from apache. Because if there is a password protected directory, for example, then the rewrite will cause the server to display that directory, which will then ask for a password, and if no valid one is supplied it will serve the 403.

Good luck!

末蓝 2024-10-15 12:58:59

ErrorDocument 指令是 Apache 的配置选项,它在发送某种错误代码时使用。如果您将所有流量重定向到 PHP,那么 PHP 就有责任以错误文档进行响应。由于默认情况下 PHP 不会执行任何操作,因此它成为的责任。

正确的做法是使用 header 发出适当的错误代码,然后回显您想要显示的任何文档。如果您愿意,您可以使用与 Apache 相同的文档。只需包含它即可。例如:

function respond404() {
  header("HTTP/1.0 404 Page Not Found");
  include("/error/404/index.html");
  exit;
}

据我所知,没有办法从 PHP 中获取这些 apache 设置,因此您要么必须手动解析 .htaccess 文件,要么必须复制Apache 设置和 PHP 应用程序之间的信息。无论如何,错误代码并不多 - 您只需要涵盖 4xx 和 5xx 范围内的错误代码。 参见此处

The ErrorDocument directives are configuration options for Apache, that it uses when sending some kind of error code. If you redirect all traffic to PHP, then it becomes PHP's responsibility to respond with an error-document. Since PHP won't do anything by default, it becomes your responsibility.

The proper thing to do, is to issue an appropriate error-code using header, and then echo out whatever document you want displayed. You can use the same documents as Apache would, if you like. Simply include it. Eg.:

function respond404() {
  header("HTTP/1.0 404 Page Not Found");
  include("/error/404/index.html");
  exit;
}

As far as I know, there is no way to get hold of these apache settings from with in PHP, so you either have to manually parse the .htaccess file, or you have to duplicate the information between your Apache settings and your PHP application. There aren't that many error codes anyway - You only need to cover those in the 4xx and 5xx range. See here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文