【发布时间】: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&cid=13 等结尾的文件和
/index.php?p=abc 没有完全加载。只有菜单 (menu.php) 和页脚 (footer.php) 正在加载。
谢谢。
【问题讨论】:
-
你好@DaveG,请在上面找到我的编辑。非常感谢您的帮助。
-
[编辑后]:问题是你把我的代码放错了地方。它应该放在其他所有内容之前(所以在 body-div 之前,甚至在所有其他标题之前。),并且在
<?php和?>之间。此外,else必须在我的代码后面删除!我的代码是补充的,但其余代码也应该执行,所以没有else! -
谢谢@DaveG。我会回复你的结果。
标签: php .htaccess http-status-code-404