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.

This entry was posted in PHP and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>