URL Rewrite in PHP
One technique to writing php websites is to run all the requests through one php page, usually index.php. This is common particularly because it allows you to add sitewide scripts (analytics etc) in one place, and also allows for nice URLs. This is by no means comprehensive, and are just a few notes I have on URL rewrites.
There are a few ways to have nice URLs in your website.
1. Use apache mod_rewrite for every 'page'
2. Use apache mod_rewrite to send all requests through a single webpage
3. Use php only with URLs that look like http://domain.com/index.php/2010/12/30/my-entry
4. For IIS, i have not tried it but I have heard there is a way to have a custom 404 page which can be used to display custom pages with nice URLs.
For method 1 and 2 you need to install apache and then mod_rewrite.
Method 1 - Usage apache mod_rewrite for every page
RewriteRule directives with regular expressions map urls to pages.
Sample httpd.conf tidbit
<IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^/?$ /index.php?p=login [L,QSA] RewriteRule ^/login/?$ /index.php?p=login [L,QSA] RewriteRule ^/login/forgot/?$ /index.php?p=login&forgot=true [L,QSA] RewriteRule ^/login/redir/([^&;/]+)/?$ /index.php?p=login&redir=$1 [L,QSA] RewriteRule ^/login/logout/?$ /index.php?p=login&logout=true [L,QSA] RewriteRule ^/login/([^&;/]+)/?$ /index.php?p=login&redir=$1 [L,QSA] RewriteRule ^/home/?$ /index.php?p=homepage [L,QSA] RewriteRule ^/company/privacy/?$ /index.php?p=privacy [L,QSA] RewriteRule ^/company/terms/?$ /index.php?p=terms [L,QSA] RewriteRule ^/company/about/?$ /index.php?p=about [L,QSA] RewriteRule ^/company/contact/?$ /index.php?p=contact [L,QSA] RewriteRule ^/user/([0123456789]+)/?$ /index.php?p=playlist&user=$1 [L,QSA] </IfModule>
Method 2 - Usage apache mod_rewrite and let php decide how to route requests
Sample httpd.conf tidbit
<Directory /srv/www/htdocs/> AllowOverride All </Directory>
.htaccess file (place .htaccess in server root)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>Method 3 - Use php to do it all
You need a way to map urls pieces to http get parameters and requests.
sample php
<?php
include_once( which_page() );
function include_which_page()
{
$pages = array();
$pages[0]['php_file'] = 'page_main.php';
$pages[0]['url_pattern'] = '^/main$';
$pages[0]['url_extract'] = array();
$pages[1]['php_file'] = 'page_contact.php';
$pages[1]['url_pattern'] = '^/company/contact$';
$pages[1]['url_extract'] = array();
$pages[2]['php_file'] = 'page_blogentry.php';
$pages[2]['url_pattern'] = '^/blog/([0-9]{4})/([0-9]{2})/([0-9]{2})/([^&;/]+)$';
$pages[2]['url_extract'] = array('p_year','p_month','p_day','p_title');
$script = basename($_SERVER['SCRIPT_NAME']);//site.php
$pieces = explode("/", $_SERVER['REQUEST_URI']);
while (isset($pieces[0]) && $pieces[0]!=$script)
array_shift($pieces);
array_shift($pieces);//remove index.php
if (end($pieces)==="" || substr(end($pieces), 0, 1)=="?" )
array_pop($pieces);//remove "" if there
$p=0;//default page
if (count($pieces)>0)
{
foreach($pages as $whichpage=>$details)
{
if (page::url_pattern_match($pieces, $details))
{
$p=$whichpage;
$matched = page::url_pattern_extract($pieces, $details);
//merge it with $_GET and $_REQUEST to be accessed later
$_REQUEST = array_merge($_REQUEST, $matched);
$_GET = array_merge($_GET , $matched);
}
else if ($pieces[0]==$pname)
{
$p=$whichpage;
}
}
}
return $pages[$p]['php_file'];
}
//---------------------------------------------------------------
function url_pattern_match(&$pieces,$entry)
{
if (!isset($entry['url_pattern']))
return false;
$sampleurl = implode("/", $pieces);
return (ereg($entry['url_pattern'], $sampleurl));//regex
}
//---------------------------------------------------------------
function url_pattern_extract(&$pieces,$entry)
{
if (!isset($entry['url_pattern']))
return array();
if (!isset($entry['url_extract']))
return array();
$sampleurl = implode("/", $pieces);
$reg =array();
ereg($entry['url_pattern'], $sampleurl,$reg);//regex
array_shift($reg);//pop $sampleurl off front
$return=array();
$extract = $entry['url_extract'];
for($i=0; $i<count($extract); $i++)
$return[$extract[$i]]=$reg[$i];
return $return;
}
?>code snippets are licensed under Creative Commons CC-By-SA 3.0 (unless otherwise specified)
|