home forums resources search newsjoinmembers: 6381
Hiveminds Network PHP Flash Java Ruby Windows Linux
 videos
 articles
 blogs
 comments
 downloads
sitemap
Hiveminds | Mon, 2008-07-14 20:42  tags:

Here is a simple script in PHP that will mimic the Apache mod_rewrite using common error pages. Call it for example 404.php and configure it to be your custom 404 error page. All you have to is to customize the last part of the page and to fill the $redirection array with the old/new paths pairs (taken from your mod_rewrite settings). If you're not using Windows, replace eregi with ereg and eregi_replace with ereg_replace (because only Windows is case insensitive with file names).

We have updated the script with isset as you suggested. We have also added a new header() statement to set explicitly the status code. With no status code, an internal redirection is done and the final displayed page will still have the status code 404 which can cause problems for people with Internet Explorer and its friendly error pages. With the explicit status code set, 404 is superceded and people will get to the right page regardless of their browser settings. Notice we've used the not very known status code 307 which is a variant of the 3xx status codes. It instructs the server to retry the request with the new URL which is equivalent to the behavior of 302 with GET. But it will also force the browser to resend any POSTed data to the new URL if the method is POST (which is not possible with other status codes). You can also use the status code 301 instead of 307 if your pages do not contain forms.

<?php
/* Add in this array the list of (old path => new path) pairs */
$redirection = array(
   
'^wiki/?(.*)$' => '/w/index.php?title=$1'
);
/* Get the URI and trim leading slashes */
$uri ltrim($_SERVER["REDIRECT_SCRIPT_NAME"], "/");
foreach (
$redirection as $key => $value)
{
   if (
eregi($key$uri))
   {
        
/* Convert the replacement string syntax - $1 -> 1 */
        /* and perform the substitution */
      
$new_uri eregi_replace($keystr_replace("$"""$value), $uri);
      break;
   }
}
if (isset(
$new_uri))
{
   
header("Status: 307");
   
header("Location: $new_uri");
   exit;
}
?>
<!-- Your 404 error page -->
<HTML>
<HEAD>
<TITLE>
Not Found
</TITLE>
</HEAD>
<BODY>
The object <tt><?php echo $uri?></tt> is not available.
</BODY>
</HTML>

printer-friendly version

Hiveminds's picture
This article brought to you by the Hiveminds Magazine - Staff. Contact us if you want to post an article or announcement anonymously
 
Web Developers Silverlight eRuby Drupal Adobe Flex PHP Silverlight Adobe Flex
 

Newsletter

Get updates on Hiveminds services, articles and downloads by signing up for the newsletter.

Editor's choice

Some of the better articles, stories and tutorials found at Hiveminds.

Find more

Find more of Hiveminds articles, stories, tutorials and user comments by searching.




Picked links

Hand picked websites and articles from around the web that provide quality reading.

page top