SEO friendly URLs with PHP and mod rewrite

Clean URL’s play an important part in Search Engine Optimization and provide a better user experience. Here’s a simple way to get SEO friendly URLs with PHP and mod rewrite.

This tutorial assumes that you are using PHP to create your website and will offer a guide for your file structure as well as a simple way to create clean URL’s when passing variables as a $_GET parameter.

Unclean URL’s with PHP

To demonstrate what we are going to do, I want to point out the simplest way to pass variables to your script in PHP. We’ll start with an example URL of:
http://mywebsite.com?uname=mike&action=message

This URL is passing a variable called “uname” with a value of “mike” and a variable called “action” with the value of “message”. The index file at the root of the domain would contain code that looks like this:

<?php 
$user_name = $_GET['uname']; 
$action = $_GET['action'];
?>

SEO friendly URL’s with PHP and mod rewrite

An example of a clean URL would be: http://mywebsite.com/members/mike/message and we can achieve that with a some changes to our code.

First, we need to start our mod rewrite by creating a file in our root called .htaccess with the following code:


  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^ index.php [L]

This turns the rewrite engine on if the module is available.
Then, it checks to see if the request is a file.
Then, it checks to see if the request is a directory.
If not, it lets the index.php file handle the request.

In our index.php file we need to access the server array which contains two variables that we need. The first one is REQUEST_URI which contains the full URL that was requested. The second one is SCRIPT_NAME which contains the path to the current script.

We will also need to define our Absolute Path in the index.php file to give us a base to load our templates.

<?php
/*Define the absolute path as the path of this file */
define('ABSPATH', dirname(__FILE__).'/');

/**
 * Get the full reqested URI and the path of this script as arrays
 */
$requestURI = explode('/', $_SERVER['REQUEST_URI']);
$scriptName = explode('/',$_SERVER['SCRIPT_NAME']);
//Loop through the script name and remove script name bits from the full URI
for($i=0;$i < count($scriptName);$i++){
     if ($requestURI[$i] == $scriptName[$i]){
          unset($requestURI[$i]);
     }
}
$commandArray = array_values($requestURI);
$command = $commandArray[0];
$parameters = array_slice($requestURI, 1);
/**
* If there is no page specified, show the index page  
*/ 
if(empty($command)){
     $command="index";
}
/**
* Load the template file if it exists  
*/
if(file_exists(ABSPATH.'/templates/'.$command.'.php'){
     require_once(ABSPATH.'/templates/'.$command.'.php');
}else{
     /*template file not found show the 404 page*/
     require_once(ABSPATH.'/templates/404.php');
}
?>

With this done, you can move on to your website files which we will place in a directory called “templates”.

This directory should contain an index.php page, a 404.php page as well as a page for each “command” that you want to support. In our example URL we need a file called members.php in our templates directory with the following code:

<?php
// Loop through parameters and echo the results
for($i=0; $i<count($parameters); $i++){
   echo $parameters[$i]."<br>";
}
?>

In this file, $parameters[0] contains the username which you can use to query your database for the appropriate user information. You can use the rest of the parameters for other include files or database queries.