Connecting Tech Pros Worldwide Forums | Help | Site Map

Show a missing page error

hsriat's Avatar
Expert
 
Join Date: Jan 2008
Location: Bath, UK
Posts: 1,609
#1: Jun 24 '08
Assume this case:
  • User requests for a page: my-page.php.
  • If the page is available, the browser shows the page.
  • But if the page is missing, the browser gets redirected to a page with address:
    missing_page.php?page=my-page.php

How can this be done?

Newbie
 
Join Date: Jun 2008
Posts: 25
#2: Jun 24 '08

re: Show a missing page error


If you are using Apache, provide .htaccess file like this:

Expand|Select|Wrap|Line Numbers
  1. ErrorDocument 401 error.php?code=401
  2. ErrorDocument 403 error.php?code=403
  3. ErrorDocument 404 error.php?code=404
  4.  
The configuration above will display error.php for HTTP error 401, 403 and 404. And error.php might looks something like this:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. switch($_GET['code']) {
  3.   case '401':
  4.     print "Error 401 - Authentication Required!";
  5.     break;
  6.   case '403':
  7.     print "Error 403 - Access Forbidden!";
  8.     break;
  9.   case '404':
  10.     print "You have requested for page " . $_SERVER['REQUEST_URI'] . " which doesn't exist.";
  11.     break;
  12. }
  13. ?>
  14.  
hsriat's Avatar
Expert
 
Join Date: Jan 2008
Location: Bath, UK
Posts: 1,609
#3: Jun 24 '08

re: Show a missing page error


So I think I need this as in .htaccess

Expand|Select|Wrap|Line Numbers
  1. ErrorDocument 404 missing_page.php?page=$_SERVER['REQUEST_URI']
realin's Avatar
Familiar Sight
 
Join Date: Feb 2007
Posts: 252
#4: Jun 24 '08

re: Show a missing page error


Quote:

Originally Posted by hsriat

So I think I need this as in .htaccess

Expand|Select|Wrap|Line Numbers
  1. ErrorDocument 404 missing_page.php?page=$_SERVER['REQUEST_URI']

$_SERVER['REQUEST_URI'] wont work in .htaccess i guess
hsriat's Avatar
Expert
 
Join Date: Jan 2008
Location: Bath, UK
Posts: 1,609
#5: Jun 24 '08

re: Show a missing page error


Quote:

Originally Posted by realin

$_SERVER['REQUEST_URI'] wont work in .htaccess i guess

pretty good point!!!

Then how to solve this?

[EDIT: Solved: Instead of using $_GET['page'] in missing_page.php, I can utilize $_SERVER['REQUEST_URI']]
Reply