This feature is not available (in other words still being worked on!) - check back soon!
Making Pretty URL's with mod_rewrite + PHP
Posted by Ben Winn in Uncategorized on June 14th, 2010

Here we have a little tutorial on using “mod_rewrite” to make pretty urls and then PHP to handle the requests of the pretty urls.

mod_rewrite provides us with a very powerful (and complex) system for getting the exact results we want. However, mod_rewrite is not part of the core Apache server, and while it’s enabled by default on most distributions (and included with the Apache Win32 binary), it may not be available on your server. That’s why this is not the be-all and end-all solution to the problem. Although the code we will use to deal with the paths in this solution will work.

To use mod_rewrite, we must place something like the following code in an .htaccess file in your web server’s root directory:


RewriteEngine On
RewriteCond %{REQUEST_FILENAME} ! -f
RewriteCond %{REQUEST_FILENAME} ! -d
RewriteRule !\.(js|ico|gif|jpg|css)$ / index.php [L]

The first two RewriteCond statements checks that the incoming request doesn’t refer to an existing file or directory. If that’s true, the RewriteRule statement will rewrite the incoming request to a request for /index.php as long as the request does not contain one of the listed filename extensions.

Once this code is in place, we can create our ideal URL (Pretty URL):

http://example.org/folder1/folder2/folder3/1234-The-Example-Page

Pretty nice eh? Well most certainly it is as this beats having ugly urls by far a long way here’s what it would look like in the ugly format:

http://example.org/index.php?f=folder1&f2=folder2&f3=folder3&title=123 The Example Page

Pretty ugly don’t you think? And if I’m honest this goes in a bad mark for Google Page Ranks so if you care about how Google see your website then I suggest going the Pretty URL way!

So now that’s my rant over, let’s move onto the PHP handling way — this should be easy for most avid PHP developers.

So we start by creating a class object:

rpclass.php

<?php
class RequestPath
{
  private $parts = array();

public function __construct()
{
if ( isset($_SERVER['PATH_INFO']))
{
$path = (substr($_SERVER['PATH_INFO'], -1) == '/') ?
substr($_SERVER['PATH_INFO'], 0, -1) :
$_SERVER['PATH_INFO'];
}
else
{
$path = (substr($_SERVER['REQUEST_URI'], -1) == '/') ?
substr($_SERVER['REQUEST_URI'], 0, -1) :
$_SERVER['REQUEST_URI'];
}
$bits = explode('/', substr($path, 1));
$parsed['action'] = array_shift($bits);
$parsed[] = $parsed['action'];

$parsed['type'] = array_shift($bits);
$parsed[] = $parsed['type'];

$parts_size = sizeof($bits);
if ( $parts_size % 2 != 0 ) {
$parts_size -= 1;
}

for ( $i = 0; $i < $parts_size; $i+=2 ) { $parsed[$bits[$i]] = $bits[$i+1]; $parsed[] = $bits[$i+1]; } if ( sizeof($bits) % 2 != 0) { $parsed[] = array_pop($bits); } $this->parts = $parsed;
}

public function __get($key)
{
   return $this->parts[$key];
}

public function __set($key, $value)
{
  $this->_parts[$key] = $value;
}

public function __isset($key)
{
   return isset($this->_parts[$key]);
}
}
?>

And simply if you want to use this class in another PHP script you would simply write this:

Example.php

<?php
require_once 'RPClass.php';
$request = new RequestPath();
echo 'Request Action: {$request->action}
';
echo 'Request Type: {$request->type}
';
echo 'Request For: {$request->for}
';
?>

And here’s what it should output:

Request Action: folder2
Request Type: folder3
Request For: 1234-The-Example-Page

Once we have pretty URLs set up and functioning, we can start to implement professional solution architectures such as the Model-View-Controller architecture, or MVC Pretty URLs are fast becoming an essential requirement for popular sites and it’s important to think about your URLs carefully, and make them as memorable–or as “guessable” –as possible.

So I hope this helps someone one way or another, if you need any help then be sure to comment below.

Tags:
Comments Off

Comments are closed.