Quickly Creating SEO Friendly Links

Over the years I have seen many different ways programmers have used mod_rewrite to make clean urls. However, I have not seen the correct way much. All you need in your .htaccess file is this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]

that will allow you to have all the /section/production/2/view/large sections you want without creating additional rewrite conditions. In your index.php script, you can access all of the uri data by simply doing this:

$vars = explode("/",$_SERVER['REQUEST_URI']);

foreach($vars as $value)
{
     echo $value." | ";
}

That will output all of the sections in the uri. Say you want to get a product number and the uri is:

/products/12345/

Grab the product number by getting the second key in the $vars array.

This concept is used in some major open source projects such as WordPress and CodeIgniter.

blog comments powered by Disqus