【问题标题】:How to add an error page in a .htaccess file如何在 .htaccess 文件中添加错误页面
【发布时间】:2012-06-07 19:33:46
【问题描述】:

下面是我的.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 错误

【问题讨论】:

  • 是否可以得到检查数据库的代码的sn-p?由于你的重写规则,重写了所有的 url 都经过 default-rewrite.php,所以问题出在代码上。
  • 所以它转到默认的重写页面并从数据库中添加页面。
  • 应该还有一段代码检查数据库中是否存在$colname_Page。
  • 非常聪明。我已经添加了代码。
  • 然后我只添加 到我的默认重写:)

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


【解决方案1】:

查看代码后,您可以添加:

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

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

【讨论】:

  • 仅发送 404 状态(即header('HTTP/1.0 404 Not Found'))不会神奇地发送ErrorDocument 指令中为 404 错误定义的错误文档。您需要在 PHP 中管理 404 文档并将其作为响应的一部分发送。
猜你喜欢
  • 1970-01-01
  • 2013-12-23
  • 1970-01-01
  • 2021-11-22
  • 2013-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-28
相关资源
最近更新 更多