Blake Loizides
Blake Loizides

Reputation: 985

How to add an error page in a .htaccess file

Below is my .htaccess file. I have a default_rewrite.php file and I get my content from a database I have created.

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

When you go to a known broken link E.G http://www.example.com/test/fgdhfuis it goes to my default rewrite page but doesn't go to a page I have stored in the database. E.g

http://www.example.com/test/home

I have tried ErrorDocument 404 /404.php but this does not do it for me.

Any advice is greatly appreciated.

Below the PHP Edit:

<?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);

  ?>

I tried

  $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";
  }

But this makes every page go to 404-error

Upvotes: 0

Views: 253

Answers (1)

px4n
px4n

Reputation: 416

Having looked at the code, you could add:

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

after the first query, and set colname_Page to equal a 404 page, that you make and put into the database. Or you can send a 404 error in PHP using header('HTTP/1.0 404 Not Found'); Apache should then use the ErroDocument 404 page that you specified in your .htaccess.

Upvotes: 1

Related Questions