Quickly Creating SEO Friendly Links
Filed under PHP
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.
July 5th, 2008 at 11:56 am
thinks its very useful
July 6th, 2008 at 12:15 pm
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?
August 7th, 2008 at 6:39 pm
@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.
October 7th, 2008 at 10:29 am
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 :/