There are several techniques to do so, and I certainly do not have the pretention to say that this one is the best of all, but it is the one I use in personal projects as it works nicely, and it is robust and dynamic.
It makes use of an .htaccess file to rewrite the urls, of a php function to parse the given parameters, and a content manager to serve the appropriate content according to them.
Before we start I'd like to make a personal thanks to Josh Moont for his precious help.
Thank You for Downloading
I'm glad that you found my work useful. If you did, it might help some other people too. So why not helping me spread the word with a tweet? You could also buy me a coffee to thank me for my code and explanations if you prefer ;).
I hope you'll enjoy your download. Regards. Jeremy.
How to setup
Step 1: The .htaccess file
As we've said in the introduction, the first element that stands in the process of rewriting is the .htaccess file. If you do not already have one on the root of your website, just create a file with no name and the extension .htaccess there. Open that file, and copy the following code in it:Ok so what does that actually mean? Simple. On line 1, we specify that we want to turn on an Apache functionality called Mod_Rewrite. Mod_rewrite is an Apache-based rewrite engine for dynamically rewriting URLs. It's built into Apache servers natively, though not enabled by default, that's why we turn it on here. The lines 3 to 12 are actually beyond the scope of this tutorial but I left them there anyway as they are still useful. In fact they allow you to create a custom error page and to redirect to that page when a specific error happens on your server, the 404 error for example. It's nicer for the user to face a custom error page than the default ones. Let's get back to our rewrite now. On line 16 and 17, we have 2 lines that tell the server NOT to rewrite the urls leading to files that exist on the server, such as images for example, or js files, stylesheets on so on. So in fact, all those files will be accessed in the old fashioned way, and the rewrite will only be focused on pages, which saves a lot of time as you'll understand later on. And Finally, on the last line, that expression specifies that every url, that is not a file, will be sent to the file index.php for analysis under the name "path". NC means it's not case sensitive, and QSA that the redirect still keeps the $_GET variables so in your urls, you can use slashes and also old fashioned query strings. (which is not the point really, but it still works...) This step marks the end of the .htaccess role in our rewrite. To summarize, it doesn't deal with files, and redirects any other url to the index.php under the name "path".
RewriteEngine On ## ERRORDOCUMENTS ### ErrorDocument 206 /Error/206 ErrorDocument 301 /Error/301 ErrorDocument 400 /Error/400 ErrorDocument 401 /Error/401 ErrorDocument 403 /Error/403 ErrorDocument 404 /Error/404 ErrorDocument 500 /Error/500 ErrorDocument 503 /Error/503 ## Page rewrite ## RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?path=$1 [NC,QSA]
Step 2: Dealing with the parameters
Now that the urls are redirected from the .htaccess, we are going to use slashes in our urls and not query strings anymore. Because this format of url is not understood by the server in the first place, we need a mechanism to parse the given parameters into a form that is going to be useful for the server. In order to do so, we'll use the function get_url_params showed below:Again I remind that there are several methods to do so, but this is the way I like to do. Explanation: First of all, on line 4, we get the "path" sent by the .htaccess file, for analysis, meaning that we split it according to the slashes and we count how many different parts it gives. Here's the principle (there are 4 different possible cases). If we do not have any parameter, the path is empty, we'll therefore display the home page.(lines 8-10) If we only have ONE parameter, it will be the page to display.(line 11-13) If we have an ODD number of parameters, the first one will be the page, and then we'll match up the other parameters by key=>values pairs.(lines14-17) And finally, if we have an EVEN number, the first one will be the page, the second one the subpage, and afther that, again we'll match up the parameters by key=>values pairs.(lines 18-22) Now that we've extracted the page to display from the parameters, we match them up by pairs (lines 25-31). As we've said in the introduction, this method is quite robust as it works with $_GET parameters too. So if there are any specified in the url, we match them up by pairs here, so they will be available later on (lines 33-36). For example, the url /resource/param1/value1/param2=value2, mixing slashes and query strings, will return $params = array("page"=>"resource","param1"=>"value1","param2"=>"value2"); To summarize this step, first we get the path from the .htaccess, we work out the page we want to display from it, and we match the other parameters by key=>values pairs. The parameters are now ready to be used by the content manager.
function get_url_params(){ //Get the query string made of slashes //Extract the asked page from those parameters case 0: $params['page'] = "home"; break; case 1: break; //odd break; default: //even break; } //Process the parameters by pairs $cpt=1;$param=""; switch($cpt){ }}} //Process the $_GET parameters if any }} return $params; }
Step 3: The Content Manager
Once we've nicely reordered the parameters, we need to display the page targeted by the given url. To do so, I like to use a content controller, in order to have an MVC structure. My controller is structured as shown:The get_url_params() function has been put into a separate class called Globals, that's why include that file on line 2. We can then call that function to get the parameters (line 3), and especially the page we want (line 4). We then have a switch case statement that figures out what file needs to be included according to the wanted page. This is how we match the parameters given in the url with the corresponding files. When you want to add a new page to your site, you just have to add a case in this content manager. In lines 34,35 and 36, the manager will finally include the pages for display. I like to use a separate header and footer, containing the code common to every page, it is easier to maintain. There we go, 3 easy steps to make your own friendly url system from scratch! No excuses for those endless and meaningless query strings any more now!
require_once("Path_to/Globals.php"); $params = Globals::get_url_params(); $module = $params['page']; switch ($module) { case "home": $page = "Path_to/home.php"; break; case "resources": $page = "Path_to/resources.php"; break; case "Error": $page = "Path_to/error.php"; break; . . . default: $page = "Path_to/home.php"; break; } } else { $page = "Path_to/home.php"; } require_once("Path_to/header.php"); require_once($page); require_once("Path_to/footer.php");
5 Responses to How to make SEO friendly urls
Leave a Reply
Other Useful Resources

My Favourite MYSQL functions
For the previous years I've been trying to point out the areas of my workflow that were taking most of my time to try to optimise them. ... En savoir plus

How to easily integrate a PayPal Checkout with PHP
PayPal is a renowned payment platform that allows you to accept online payments on your site, by taking care of all the money transactions ... En savoir plus

jQuery FancyBox plugin
The FancyBox is one of the plugin that I like the most. It's so easy to use and renders really nicely, especially with images. It's a Mac li... En savoir plus
Thanks I wil ltry this at my Tibia aCcount website thanks so much I hope it works with no problems. Its just htat I dont know where to put the php codes.
Great tutorial.
Thanks for the tutorial, you explained it very clearly!
Thanks a lot. I'm mainly using WordPress so I won't need it but once I start making custom websites I'd definitely use this.
it not work in my local machine an web server. what's wrong?