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.

4 Responses to “Quickly Creating SEO Friendly Links”

  1. abbouh Says:

    thinks its very useful

  2. Kasper Says:

    Hmm I don’t quite understand how can I implement this if I currently am using links as index.php?main=view&id=1 links.
    Just stuff in this code and it’ll work?

  3. dawnerd Says:

    @Kasper

    Depends on how you are getting the value of main and id. If you convert then to $main and $id (assuming you are), then you should be able to drop this in and not spend too much time converting your current system over.

  4. Buch Says:

    Heya, sounds nice but I don’t really understand how this should work :O I mean if i have a switch($_GET[a]) {
    case “test”;
    echo “lol”;
    $test = $_GET[id];
    echo “$test”;
    break;
    }

    How should I convert that? would the link then look like a/test? normal would look like ?a=test.

    But how should i convert them to variables with $?

    I don’t really get it :/

Leave a Reply






Copyright 2008 Troy Whiteley & OjaiSoft

CSS | PHP | XHTML/HTML | JavaScript | Featured Sites

Home | Labs | License | RSS | Log in