// you’re reading...

PHP

SEO friendly URL in PHP

SEO friendly

When I started implementing mod_rewrite in websites I had a problem in PHP as how to make a SEO friendly URL. All tutorials were geared towards how to implement mod_rewrite, about modifying .htaccess files, but none treated how to make the urls friendly with dynamic content. For example I have a news section which are pulled from the database, but how can I make the URL friendly with the title.

For example, my problem was to convert titles like "Barca rejects FIFA statement on Olympics row" into "barca-rejects-fifa-statement-on-olympics-row.html". The trick has to work also with french titles (I'am sure my friends at Nexen will appreciate ;-)) like "Guantanamo: le chauffeur de Ben Laden plaide non coupable à l'ouverture de son procès" into "guantanamo-le-chauffeur-de-ben-laden-plaide-non-coupable-a-l-ouverture-de-son-proces.html", removing all french accentuated characters.

Hopefully I came across this function which I think might help you too :

 
function friendlyURL($string){
	$string = preg_replace("`\[.*\]`U","",$string);
	$string = preg_replace('`&(amp;)?#?[a-z0-9]+;`i','-',$string);
	$string = htmlentities($string, ENT_COMPAT, 'utf-8');
	$string = preg_replace( "`&([a-z])(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig|quot|rsquo);`i","\\1", $string );
	$string = preg_replace( array("`[^a-z0-9]`i","`[-]+`") , "-", $string);
	return strtolower(trim($string, '-'));
}
 

Use :

 
$myFriendlyURL = friendlyURL("Barca rejects FIFA statement on Olympics row");
echo $myFriendlyURL; // will echo barca-rejects-fifa-statement-on-olympics-row
 

You just have to concatenate anything you want with the string afterwards. For french users, make sure your string is encoded in UTF-8 else it won't work.

If you're using the Smarty templating engine I made a little modifier which you can download. Extract the zip file in your Smarty/libs/plugins/ folder and to use it in your templates :

 
{"Barca rejects FIFA statement on Olympics row" | friendlyURL}
 

Or if your string is in a variable :

 
{$yourString | friendlyURL}
 
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • del.icio.us
  • Facebook
  • Google
  • StumbleUpon
  • Mixx
  • Ma.gnolia
  • Technorati
  • Netvouz
  • Reddit
  • Propeller
  • Slashdot
  • description

Discussion

6 comments for “SEO friendly URL in PHP”

  1. this is my function for friendly Romanian URLS:

    public function setId( $id, $nume )
    {
    $nume = strToLower( $nume );
    $nume = str_replace( array( ” “, “ş”, “Ş”, “Ţ”, “ţ”, “ă”, “î”, “â” ),
    array( “-”, “s”, “s”, “t”, “t”, “a”, “i”, “a” ),
    $nume );
    $nume = preg_replace( “/[^a-zA-Z0-9\-]/”, “-”, $nume );
    $nume = urlEncode( $nume );
    $url = $id . “-” . $nume . ‘.html’;
    return $url;
    }

    Posted by Dumitru | July 25, 2008, 12:22 pm
  2. thanks for the tip Dumitru ;-)

    Posted by asvin | July 25, 2008, 12:36 pm
  3. Thx for the function.

    What is your experience the easiest way to implement reading of seo friendly urls? (e.g. mod_rewrite)

    Posted by materix | July 29, 2008, 3:51 am
  4. The easiest way to implement it is to use htaccess files. Create one and simply put these :

    RewriteEngine on
    RewriteRule ^my-friendly-url.html$ myphppage.php?id=2

    Posted by asvin | July 29, 2008, 9:44 am
  5. Do you know if it possible to build the .htaccess file automatically?
    It is a bit tedious to update the file with a new RewriteRule, every time a new page is added.

    Posted by materix | July 29, 2008, 4:24 pm
  6. materix,

    Maybe you’re talking about RewriteMap.
    Check out this link :
    http://www.onlamp.com/pub/a/apache/2005/04/28/apacheckbk.html

    Posted by asvin | July 29, 2008, 6:36 pm

No Comments yet, your thoughts are welcome