I have been playing around with Apaches mod_rewrite on my other site http://www.2big2send.com and i must admit its a whole lot easier than i first thought..
Here is a quick guide for most general use:
Create a new file in your main directory (public_html for cPanel or httpdocs for plesk – not sure about other ones) called .htaccess (Dont forget the ‘.’ dot before the name – you may already have one of these file, thats ok, just edit it and follow the guide below.
RewriteEngine On # Check if the file or directory actually exists - if it does we dont want to redirect RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # Pass the rewritten URL onto index.php with a $_GET['url'] parameter RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
What the above Apache code does is firstly check the actual URL and ensure that is it not a file or directory, that actually exists.
Then it passes the request onto index.php for handling
Here is the index.php code with the handler
function handleRequest($urlString) { $url = array(); // Split apart the URL String on the forward slashes. $url = explode('/', $urlString); switch($url[0]) { case 'home': include_once('home.php'); break; case 'news': include_once('news.php'); break; default: include_once('home.php'); break; } } $incommingURL = (isset($_GET['url']) ? $_GET['url'] : ''); handleRequest($incommingURL);
So… The handleRequest function takes in the URL GET parameter we setup in the .htaccess file.
We split that apart on the forward slashes so it can be easily expanded.
I have assumes that the first part of the URL will be the base page; so for example: http://www.domain.com/home will cause the home.php page to be included.
If the URL was http://www.domain.com/news/page-1 then the news.php page would be included, and then you would do some processing on the news.php page to handle the /page-1 part of the URL ($url[1]).
Here is the part you are most interested in – the
Mod_Rewrite and PHP Demo
and
Mod_Rewrite.zip Download
There are a few Gotchas that I found; the main one being that you must edit your httpd.conf file, you will find a <Directory /> part where AllowOverride None is listed – you MUST change this part to AllowOverride All or mod_rewrite will not work.
There are others – but i will have to answer them as you come across them as i cant remember off the top of my head.

Thanks man, this solution was all I need
http://www.mysite.com/news –> http://www.mysite.com/index.php?page=news
with relative css and image files intact!!!
awesome stuff
thanks for the great info
very great stuff for everyone who has work with php…
great work!!
i always have a problem to direct 301 from htacess to more than page to the home page with one order i mean one line comand is that possible ?
Thank you man, this is so useful.
hi very nice information.
but please tell me how can i do this without mod_rewrite.
Please tell me if you know.
Hi Dotiv – im not sure its possible without Mod_Rewrite on Apache.
on Nginx i know its possble but in a different way.
Superb tutorial. It has just taken 3 days of googling before coming across this – So many, many thanks
I’d like to know if it is possible to call include files instead of the just echoing the ?
<?php
$pageNumber = (isset($url[1]) ? $url[1] : 'page-1');
$page = explode('-', $pageNumber);
echo 'Page: ‘.$page[1].”;
?>
If so, how would the markup look like?