SEO friendly URL’s using .htaccess and PHP regular expressions

“If there is any sin more deadly than envy, it is being pleased at being envied.” Richard Armour

i do wanted for quite sometime now to develop a website with SEO friendly capabilities and not the traditional method. unforunately i’m not very successful about it. i found a tutorial for php regular expressions but no for .htaccess.

i’m constantly using wordpress since it has a SEO friendly url feature. but i said to myself that i don’t need to rely that much on wordpress.

last night i tried searching for it again and tried something new. somehow i was able to make it work. the first one doesn’t allow the image to be posted, .htaccess also does this, it’s called hotlinking, this feature is used to conserved webside bandwith.

here’s the code for my PHP regular expression.

<?php
/*
Modified by: Immanuel Dennis H. Lopez
*/
$request = $_SERVER['REQUEST_URI'];
$filename = $_SERVER['SCRIPT_NAME'];
$request = substr($request, strrpos($filename, ‘/’) + 1);
while(substr($request, -1) == ‘/’)
  $request = substr($request, 0, -1);
 $request = explode(‘/’, $request);
foreach($request as $key => $value)
  if($value == ”)
    array_splice($request, $key, 1);
  //print_r($request);
  if ($request[0]==”articles”)
  {
   //echo $request[0];
   include(“articles.php”);
  }
  else if ($request[0]==”about-us”)
  {
   include(“about-us.php”);
  }
//print_r($request); //This will show us all the parameters passed in the URL
?>

and here’s the code for the .htaccess.

# BEGIN
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /!labs/cleanurls/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /!labs/cleanurls/index.php [L]
</IfModule>
# END

the last RewriteRule part should be changed to the website address. i’m currently running it in xampp and it runs in localhost.

Advertisement

~ by sunny on July 20, 2010.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.