URL rewriting let you hide all your ugly variable ridden URLs and instead present to the world clean, static URLs. For example (taken from ozziebuy.com.au), the URL:
http://www.ozziebuy.com.au/document.php?Filename=how_to_register
can take the form:
http://www.ozziebuy.com.au/how_to_register.htm
There are many advantages to using dynamic URLs. As they let your webpages appear as static HTML pages, search engines are more likely to index them (some search engines get jittery at indexing dynamic pages). They're more intuitive and user-friendlier for users. It allows you to put all your content into a database and greatly reduces the number of PHP pages to maintain (I'm a big fan of separating content and code). And crucially, it allows you (or your clients) to edit your website via a web based content management system rather than depending on HTML editors and ftp programs.
Sold to the idea yet? In this tutorial, I'll reduce an entire HTML website to a single PHP page. The first step is to create a MySQL table 'page' that will store your webpage content and for the sake of this exercise, insert one sample page:
CREATE TABLE `page` (
`PageId` int(11) NOT NULL auto_increment,
`Title` varchar(100) NOT NULL default '',
`Filename` varchar(30) NOT NULL default '',
`Content` text NOT NULL,
PRIMARY KEY (`PageId`)
) TYPE=MyISAM PACK_KEYS=0 AUTO_INCREMENT=20 ;
INSERT INTO page ( Title , Filename , Content )
VALUES ( 'Homepage', 'index', 'Content to go on homepage');
|
Next is to create a new text file and copy and paste the following content. Then save the text file as .htaccess (note - it mustn't have a .txt extension):
.htaccess
RewriteEngine on
RewriteRule ^([0-9a-zA-Z-_]*).htm$ index.php?Filename=$1 [L]
|
What this does is use a regular expression ^([0-9a-zA-Z\-\_]*).htm$ to look for any webpage that takes the form filename.htm. It then extracts the first part of the filename (whatever is in front of the period) and loads instead a page index.php?Filename=$1 where it replaces the $1 with the first part of the filename. In other words, if you visited the URL www.ozziebuy.com.au/how_to_register.htm, the regular expression would extract how_to_register and instead load the page www.ozziebuy.com.au/document.php?Filename=how_to_register. Now we need the webpage index.php:
index.php
// include and instantiate a database class
// see http://www.tipsntutorials.com/tutorials/PHP/76/ for more code on this
include ('includes/database.php');
$db = new Database;
// if no Filename is defined, send them to the homepage (alternatively you could send them to an error page)
if (!isset($_GET['Filename']) || !$_GET['Filename'])
$Filename = 'index';
else
$Filename = $_GET['Filename'];
// retrieve page content from database
$Query = "SELECT * FROM page WHERE Filename = '$Filename'";
$db->query($Query);
// only show content if we found something
if ($db->numRows())
{
// display title and content of this page
$db->singleRecord();
echo "<h1>".$db->Record['Title']."</h1>rn";
echo $db->Record['Content'];
} // end if we have page content
else
{
echo "<h1>Page Error</h1>rn There is no page at this address.";
} // end if we have no page content
|
Apologies but I use a database class explained in another tutorial for all my database functions (saves me repeating lots of code). If no Filename is specified in the URL, I assume one myself - 'index' in this case. Of course, you would have to have a page in your database with Filename 'index' for this to work. Alternatively you might want to send them to an error page or sitemap. Then it's a simple matter of finding the page in the database that matches the filename. If a page is found, display the title and content. If not, display an error message. And there you have it - all your HTML pages reduced to a PHP page, a .htaccess file and a MySQL database. Now all you have to do is whip up a content management system... easy! :-)
Comment by Rhys on 2007-06-17
Thank you for this tutorial. The idea of simplifying the URL to be more user friendly, and keeping things simple with a database is exciting. This should improve my websites. Thanks again!!
Comment by philips on 2010-06-18
nice post,
but, what if i want database queries based on primary key (page id) instead of filename, cos i think it\\\\\\\'s bad practice to query based of title, filename, or something that\\\\\\\'s not a primary key.
i\\\\\\\'ve found solutions for this:
the URL goes something like this:
http://www.example.com/article/12/title-here
but i don\\\\\\\'t want to display id, so the url would look like this:
http://www.example.com/article/title-here
how can i do this...???
thx in advance!
Comment by Solyn on 2011-07-04
This forum needed skhaing up and you?ve just done that. Great post!
Comment by soundhar on 2012-01-11
thank you for this tutorial. it helps a lot
Comment by soundhar on 2012-01-11
thank you for this tutorial. it helps a lot
Comment by tony on 2011-02-08
how about images and css? how to include them?
Comment by discount Michael Jordan Shoes on 2010-09-17
Air Jordan Shoes are always so attractive, good quality can ensure the wearer\'s feet, not only that, the Michael Jordan Shoes has become a fashion, many young people are very fanatical Air Jordan. High price so many people can realize their dreams. Well now, our products directly from third-party products, so we can provide cheap Jordan Basketball Shoes and New Jordan Shoes, high quality, excellent service, come on, give yourself a choice.
We offerAir Jordan 1, Air Jordan 2, Air Jordan 3,Air Jordan 4, Air Jordan 5, Air Jordan 6, Air Jordan 7, Air Jordan 8, Air Jordan 9, Air Jordan 10, Air Jordan 11 and so on!
|