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.
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
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
and
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.