Its not only a SEO and user friendly functionality; Url Rewriting techniques are also a good approach to make your website safer. By using Url Rewriting, you will be hiding your script extensions and website paths, avoiding sql injection hacks.
As we are talking about PHP here, this is implemented with the mod_rewrite directive on Apache. This extension is available by default in almost any hosting service, ready for you to use it in your website.
WordPress and other CMS’s are ready to use url rewriting, and you just need to enable the option on control panel. But when you are dealing with a custom CMS, for example, you will need to make your own url rewriting structure. There are two paths for accomplishing that: using a rule on .htaccess for every single url on your website – which is impracticable, but sometimes needed for some inherited urls and scripts that you can’t change; and by using a generic rule with an intelligent website structure, as used in WordPress CMS.
On this post you will learn a practical way for implementing url rewriting on your website with this generic single-rule model.
mod_rewrite and .htaccess
Before going any further, you need to understand how url rewriting schema works. We need to establish rules on the .htaccess file, which is situated on the www directory root (you can also use a separated .htaccess file in any subdirectory, establishing rules specific for that directory). The rules are defined using regular expressions. The image below explains how the request is processed by Apache and PHP. The first request doesn’t uses rewriting, and just by looking at the url you will say “I see what you did here, developer…”. The second request uses url rewriting, letting you think that its just a regular html file.
Now lets see a functional example. Lets assume we have a directory structure like this on our www folder: “abc/def/ghi” (accessing it via http, the url would be http://mysite.com/abc/def/ghi ). So we would like that the files on this folder could be accessed in a simpler path: /xyz . In other words, rather than using the url http://mysite.com/abc/def/file.html, I want to use: http://mysite.com/xyz/file.html (and this “xyz” directory actually doesn’t exists).
Our .htaccess file would be like this:
RewriteEngine On RewriteBase / RewriteRule ^/xyz(.*) /abc/def/ghi$1
For more details about all the possible rules, and what each item means, its good to have a look on the Apache’s mod_rewrite documentation: http://httpd.apache.org/docs/current/mod/mod_rewrite.html . This link also has good basic tips: http://php.refulz.com/201105/url-rewriting-with-apache/ .
First Step – .htaccess
As we talked before, we can use use a lot of rules in our htaccess, but the simplest and most elegant way for working with url rewriting is by using a generic rule and a controller (php) which will manage the requests and grab the right page for that request. The content bellow is the .htaccess file used by WordPress, the same we will be using for our example:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Basically, this .htaccess specifies that: every request, having “/” as base, which is not a existing file or directory, shall be passed to /index.php file. This file will be responsible for treating all requests, any way it wants. A controller.
Remember two things:
- Existing directories will not be affected. Example: If you have a “blog” directory you will not be able to have a rewriting like “/blog” for a script.
- You have to take care of 404 errors, because as you are getting all requests for files and dirs that don’t exist, the “404″ error will not happen anymore. Everything will come to index.php and you have to deal with “not found” requests on your controller – just like WordPress does.
Step 2 – PHP Controller and Directory Structure
The file bellow will be our controller, responsible for taking care of all requests. On this example, will make this through includes.
As you can see, the logic is very simple. A request to “/contact” , for example, will check if there’s a module called contact – this is done by checking if there’s a “modules/contato/index.php” or “modules/contato.php” file.
It’s important to check if the file exists before including it – only this way is possible to know if we must send the user to a “404″ error.
For adding a new page on your website, you will need to create a file (or directory, and then an index.php file inside it) inside the “modules” directory, and the script will be immediately accessible via http://yoursite.com/yournewmodule - easy as that. A folder with “yournewmodule” cannot exists on the root directory, worth remembering.
That “modules/yournewmodule.php” file will be a controller in an MVC environment. You can do your stuff there and show a template file, for example.
If you want submodules, like “mysite.com/about/company”, you shall check for that $path array, which will contains all the “pieces” of the request. Just make a switch and you are ready to go. Its important to make this $path variable available on all script scope.
Bellow is how our directory structure would be:
/ /includes /includes/config.php /modules /contato.php /404.php /.htaccess /index.php
Starting from here, you can construct a good framework for your php web applications. Good luck

