php - 如何在.htaccess文件中添加错误页面

标签 php html .htaccess mod-rewrite error-handling

以下是我的.htaccess文件。我有一个default_rewrite.php文件,我从已创建的数据库中获取了内容。

RewriteEngine on
RewriteRule ^/?([a-z0-9-_]*)$ default-rewrite.php?page=$1 [L]

当您转到已知的断开链接E.G http://www.example.com/test/fgdhfuis时,它将转到我的默认重写页面,但不会转到我存储在数据库中的页面。例如
http://www.example.com/test/home

我已经尝试过ErrorDocument 404 /404.php,但这对我没有帮助。

任何意见是极大的赞赏。

在PHP编辑下面:
<?php
  if (!function_exists("GetSQLValueString")) {
  function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
  {
  if (PHP_VERSION < 6) {
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
  case "text":
  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  break;    
  case "long":
  case "int":
  $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  break;
  case "double":
  $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  break;
  case "date":
  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  break;
  case "defined":
  $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  break;
  }
  return $theValue;
  }
  }

  $colname_Page = "home";
  if (isset($_GET['page'])) {
  $colname_Page = $_GET['page'];
  if($colname_Page == "")
  $colname_Page = "home";
  }

  mysql_select_db($database_VRMOnline, $VRMOnline);
  $query_Page = sprintf("SELECT * FROM Page WHERE PageName = %s", GetSQLValueString($colname_Page, "text"));
  $Page = mysql_query($query_Page, $VRMOnline) or die(mysql_error());
  $row_Page = mysql_fetch_assoc($Page);
  $totalRows_Page = mysql_num_rows($Page);

  mysql_select_db($database_VRMOnline, $VRMOnline);
  $query_MetaData = "SELECT * FROM MetaData WHERE MetaDataID = 1";
  $MetaData = mysql_query($query_MetaData, $VRMOnline) or die(mysql_error());
  $row_MetaData = mysql_fetch_assoc($MetaData);
  $totalRows_MetaData = mysql_num_rows($MetaData);

  ?>

我试过了
  $colname_Page = "home";
  if (isset($_GET['page'])) {
  $colname_Page = $_GET['page'];
  if($colname_Page == "")
  $colname_Page = "home";
  if ($totalRows_Page == 0)
  {
  $colname_Page = "404-error";
  }

但这会使每个页面都出现404错误

最佳答案

看完代码,您可以添加:

if ($totalRows_Page == 0)
{
 // 404 Page Logic
}

在第一个查询之后,并将您创建并放入数据库的colname_Page设置为404页面。或者,您可以使用 header (“HTTP/1.0 404 Not Found”)在PHP中发送404错误。 然后,Apache应该使用在.htaccess中指定的ErroDocument 404页面。

关于php - 如何在.htaccess文件中添加错误页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10938656/

相关文章:

html - 单元格之间具有自动宽度和边距的 CSS 垂直列表

.htaccess - .htaccess-尝试强制使用https时“重定向过多”

.htaccess - gzip 工作但 YSlow 表明它不是

php - 如何简化此数据库更新查询 (php)

html - Overflow-y auto 在这种情况下不起作用

php - 从 mysql 中获取图像并在 php 中使用 timthumb 调整大小

jquery - 转换完成后监听新事件

.htaccess - 将 MyBB 页面重定向到 vBulletin

php - 如何在 PHPExcel 图表中发送静态工作表数据

php - php导出excel文件的问题