404错误页面不适用于错误的动态页面

时间:2014-10-22 10:07:19

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

Hello尊敬的程序员,

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

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

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

我之前已将404代码添加到.htaccess,但它影响了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>

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

热切期待你的帮助。

非常感谢你。

/ **在@ 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&cid=13和...结尾 /index.php?p=abc未完全加载。只加载菜单(menu.php)和页脚(footer.php)。

谢谢。

1 个答案:

答案 0 :(得分:0)

您可以在index.php中使用file_exists() - php函数(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();
  }
}