【问题标题】:404 Error Page not working for wrong dynamic pages404错误页面不适用于错误的动态页面
【发布时间】:2014-12-17 18:16:13
【问题描述】:

尊敬的程序员们,

首先,我的网站最近完成了,但程序员拒绝提供支持,理由是它是合同的一部分。但是,我只想解决这个单一的问题,我可以管理这个站点,直到将来升级。

网站页面具有以下任何格式:

http://example.com/index.php?p=course&cid=xx
http://example.com/index.php?service&sid=xx
http://example.com/index.php?p=about-us

我在该位置有一个 404 错误页面:

/include/404.php

它会显示为

http://example.com/index.php?p=404.php

现在我想设置 404 错误页面以在错误页面显示时显示: http://example.com/index.php?p=about-us2.php

当输入错误的页面时,会出现以下情况:

Warning: include(include/about-us2.php): failed to open stream: No such file or directory in /home/hanpan5/public_html/testing/index.php on line 127

Warning: include(include/about-us2.php): failed to open stream: No such file or directory in /home/hanpan5/public_html/testing/index.php on line 127

Warning: include(): Failed opening 'include/about-us2.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/hanpan5/public_html/testing/index.php on line 127

我之前在 .htaccess 中添加了 404 代码,但是它影响了托管在 http://blog.example.com 上的博客上的页面 从 .htaccess 中删除代码后,博客页面又可以正常工作了。

我还注意到index.php中有一些代码:

<div class="body">

        <?php include('include/menu.php'); ?>

        <?php
        if(!isset($_GET['p']) && @$_GET['p'] == '')
        {
        include('include/slider.php'); ?>

        <?php include('include/services.php'); ?>

        <?php include('include/strategy.php'); ?>

        <?php include('include/testimonial.php');
        include('include/twitterfeeds.php');
        }else
        {
          $p = $_GET['p'];
          include('include/'.$p.'.php');
        }
        ?>

        <?php include('include/footer.php'); ?>

    </div>

无论如何,我可以在上面的 if 语句中添加 include('include/404.php'); 以帮助重定向。或者我能做些什么来解决这个问题?

热切期待您的帮助。

非常感谢您。

/** 在@DaveG 的第一个答案之后编辑 1 **/ 你好 DaveG,这是我目前对你的解决方案的实现:

<div class="body">

        <?php

        include('include/menu.php');

        if(isset($_GET['p']))
            {
              $p = $_GET['p'];
              if(!file_exists('include/'.$p.'.php'))
              {
                header('HTTP/1.0 404 Not Found');
                include('include/404.php');
                exit();
              }
            }
        else

        if(!isset($_GET['p']) && @$_GET['p'] == '')
        {
        include('include/slider.php'); ?>

        <?php include('include/services.php'); ?>

        <?php include('include/strategy.php'); ?>

        <?php include('include/testimonial.php');
        include('include/twitterfeeds.php');
        }
        else
        if(isset($_GET['p']))
        {
          $p = $_GET['p'];
          include('include/'.$p.'.php');
        }
        ?>

        <?php include('include/footer.php'); ?>

    </div>

只有索引页 (/index.php) 正在完全加载。

但是,以 /index.php?p=course&amp;cid=13 等结尾的文件和 /index.php?p=abc 没有完全加载。只有菜单 (menu.php) 和页脚 (footer.php) 正在加载。

谢谢。

【问题讨论】:

  • 你好@DaveG,请在上面找到我的编辑。非常感谢您的帮助。
  • [编辑后]:问题是你把我的代码放错了地方。它应该放在其他所有内容之前(所以在 body-div 之前,甚至在所有其他标题之前。),并且在 &lt;?php?&gt; 之间。此外,else 必须在我的代码后面删除!我的代码是补充的,但其余代码也应该执行,所以没有else
  • 谢谢@DaveG。我会回复你的结果。

标签: php .htaccess http-status-code-404


【解决方案1】:

您可以在 index.php 中使用 php 的 file_exists()-function (http://php.net/manual/en/function.file-exists.php) 检查文件是否存在,然后您可以返回 404 代码:

<?php header('HTTP/1.0 404 Not Found'); ?>

注意:这必须作为第一个输出命令完成,所以首先检查存在,然后返回标题,然后继续其余代码!

总共:

if(isset($_GET['p']))
{
  $p = $_GET['p'];
  if(!file_exists('include/'.$p.'.php'))
  {
    header('HTTP/1.0 404 Not Found');
    include('include/404.php');
    exit();
  }
}

【讨论】:

  • 你好@DaveG,请看我上面的编辑。感谢您的帮助。
  • 谢谢@DaveG。我已将您的解决方案的实施添加到您的答案中。您可以对其进行同行评审,以便其他人看到它。非常感谢你,上帝保佑你。
猜你喜欢
  • 2014-08-04
  • 2012-03-26
  • 2013-09-19
  • 1970-01-01
  • 1970-01-01
  • 2015-06-22
  • 2014-07-20
  • 1970-01-01
相关资源
最近更新 更多