Zend - The PHP Company




Miscellaneous

Add Code


langard  

Type: code fragment
Added by: langard
Entered: 16/07/2001
Last modified: 07/12/2000
Rating: - (fewer than 3 votes)
Views: 5223
CUSTOM 404 - HTM TO PHP SCRIPT This custom .404 script performs some valuable, practical functions: 1). For Web developers who are transitioning large, static HTML sites to much more powerful PHP versions it allows you to replace pages gradually and TRANSPARENTLY without sacrificing your internal or external link integrity. All links to your pages can remain the same (i.e. .htm, .html, etc) even AFTER you replace them with same-name .php versions. What this does is prevent you from losing any position in the search engines and also keeps all your internal and incoming links from other sites operating and active. 2). You don't have to use the (Apache) Mod_Rewrite module, Force_Type directives or even have access to your .conf file in order to use it. This is especially useful for those running several virtual domains on the same server and need separate rules for each. 3). When your new PHP pages are in place, you can still submit them for re-indexing with the search engines and directories using the (more friendly and common) .htm extensions. 4). By leaving this .404 script in place, you can simply add different header locations or file includes ($page.php) to it later that redirect to, say, your database scripts or other PHP pages that perform different functions. In this way you can create literally thousands of (what appear to be) normal, static .html pages which (actually) do not exist, but are php-created on the fly.


<?php 

/*

EXPLANATION: This custom .404 script performs some valuable, practical functions: 
1). For Web developers who are transitioning large, static HTML sites to much more powerful PHP versions it allows you to replace pages gradually and TRANSPARENTLY without sacrificing your internal or external link integrity. All links to your pages can remain the same (i.e. .htm, .html, etc) even AFTER you replace them with same-name .php versions. What this does is prevent you from losing any position in the search engines and also keeps all your internal and incoming links from other sites operating and active.
2). You don't have to use the (Apache) Mod_Rewrite module, Force_Type directives or even have access to your .conf file in order to use it. This is especially useful for those running several virtual domains on the same server and need separate rules for each.
3). When your new PHP pages are in place, you can still submit them for re-indexing with the search engines and directories using the (more friendly and common) .htm extensions.
4). By leaving this .404 script in place, you can simply add different header locations or file includes ($page.php) to it later that redirect to, say, your database scripts or other PHP pages that perform different functions. In this way you can create literally thousands of (what appear to be) normal, static .html pages which (actually) do not exist, but are php-created on the fly.
 
NOTES: This script is written to be used as a CUSTOM .404 SCRIPT. For instance, you can simply add a line in your .htaccess file pointing to it like this:
ErrorDocument 404 ScriptName.php (where ScriptName is whatever you decide to name the following PHP document, placed in your root directory). If you get any errors in your log, you might want to also redirect 405 and 500 ErrorDocuments to this same script.

IN THE CASE BELOW $url_array[1] means the page I am parsing is in the root directory (i.e. http://www.domain.com/somepage.htm). If it is in a subdirectory, you might use $url_array[2] (i.e. for a page like http://www.domain.com/somedirectory/somepage.htm) or whatever it takes to get the actual page from your URL link structure. Any mis-typed or completely false URL is re-directed to the 'yourdomain.com' home page in this example which, of course, should be changed to your own.

This work fine in PHP4. Haven't tried it in PHP3 but should be no problem at all.
You can see a simple working version at our corporate site at: http://www.myu4ea.com/
Comments/suggestions: webmaster@myu4ea.com 
*/


//CUSTOM 404 HTM TO PHP SCRIPT

$url_array=explode("/",$REQUEST_URI); //BREAK UP THE URL PATH 

$get_page $url_array[1];            //GET THE LAST PAGE IN YOUR URL
$split_page explode(".",$get_page); //SPLIT THE PAGE NAME FROM ANY EXTENSION

if($split_page[1]){                   //IF THERE IS AN EXTENSION...
$page=$split_page[0];                 //USE PAGE NAME WITHOUT EXTENSION
    
if(file_exists("$page.php")){    //MAKE SURE THE PAGE EXISTS
    
include ("$page.php");           //CALL THE PHP VERSION AS AN INCLUDE
    
}
    if(!
file_exists("$page.php")){   //IF PAGE NON-EXISTENT, BACK TO HOME
    
header ("location: http://www.yourdomain.com");
    }
}elseif(!
$split_page[1]){             //IF THERE IS NO EXTENTION
$page=$get_page;                      //USE PAGE NAME WITHOUT EXTENSION
    
if(file_exists("$page.php")){    //MAKE SURE THE PAGE EXISTS
    
include ("$page.php");           //CALL THE PHP VERSION AS AN INCLUDE
    
}
    if(!
file_exists("$page.php")){   //IF PAGE NON-EXISTENT, BACK TO HOME
    
header ("location: http://www.yourdomain.com");
    }
}

?>


Usage Example


See the example


Rate This Script





Search



This Category All Categories