Zend - The PHP Company




Miscellaneous

Add Code


URl rewitring without mod_rewrite (update for PHPSSESID correct support)  

Type: code fragment
Added by: cyberdark
Entered: 27/08/2001
Last modified: 01/11/2000
Rating: **** (3 votes)
Views: 6652
Simple example of how to manage variable in the way the URL seems to be a directory, being in fact call to a PHP script


This small piece of code can be very useful for a few things:

1- Make the url sensefull
2- Be able to index your dynamic website in some search engines that does not index dymanmic websites (FE, Lycos)
3- Make your URLs look smart.

The idea is this:

http://www.mihost.com/script.php?id=23&section=56&SID=32453235532345

can be written as

http://www.mihost.com/script.php/23/56/32453235532345/

how, you will say?

First, include this piece of code in all your pages:

if (!$id) {

    //So id has not been received as a parameter.... (in case you want to keep the old URL also)
    
    //This are the variables I expected the URL to have, in an array
    
    $myexp=explode(" ","id section SID");
    
    include ("resolutor.php");

}

The file resolutor.php is the one which controls this new parameters:

<?
        
        $vals
=explode("/",$REQUEST_URI);
        
        
$c=0;
        
        array(
$tvals);
        
        for (
$i=0$i<count($vals); $i++) {
            
            if (
$vals[$i]!="" && $vals[$i]!="/" && $vals[i][0]!="?")  {
                
$tvals[$c]=$vals[$i];
                
$c++;
            }
        }    
        
        for (
$i=1$i<count($tvals); $i++)
            if (
count($myexp)>=$i
                eval(
"$".$myexp[$i-1]."="".$tvals[$i]."";");
        
?>

What "resolutor.php" does is parse the REQUEST_URI, asigning dynamically the values of the expected variables. Also avoids problems with empty variables. If you want to keep empty variables, modify the code.

Renember that it needs to access global variables (in case you put it inside a function).

The code works with PHP3 (as you can see in the example site), and with PHP4.

With some configuration options in Apache the URL could also be written as

http://www.mihost.com/script/23/56/32453235532345/

This tip is VERY IMPORTANT for e-commerce based web site, or sites (as mine) that use intensibly data taken from DB.

One of my websites shows in many pages the whole thing working.

Hope you find this useful :)

Notes:

-This will not work in CGI mode (PHP with Apache). (Thanks to Jeffery R. Catton)

Changelog:

v1.1 - Update for PHPSESSID support under machines with auto url rewriting (ex http://www.myhost.com/page.php3/variable/?PPHPSESSID=94849), wich old code caused to work unproperly
v1.0 - Initial Release


Usage Example


See the example


Rate This Script





Search



This Category All Categories