SEO Friendly URL in PHP

php seo friendly url function

For anybody that has at least some knowledge about SEO, it’s clear how important role URL structure has for SEO success. Seo-friendly URLs are one of the most important factors in getting the most out of your SEO. When you create a clear URL structure of your website page, you have more chances to get people to click on your link and generate traffic for your website. For that purpose, here you can see how you can use SEO friendly URL in PHP with htaccess file.

Table of Contents

What is PHP?

PHP acronym stands for Hypertext Preprocessor.  This scripting language that is embedded in HTML is open-source, general-purpose, and widely used.  Programmers use it for website development, with a major contribution in creating interactive and dynamic websites.

SEO friendly URL PHP

When you start implementing mod_rewrite in websites you can have a problem in PHP to figure out how to make a SEO friendly URL. Tutorials are mostly focused on how to implement mod_rewrite, about modifying .htaccess files, but none show how to make the URLs friendly with dynamic content. For example, you can have a news section that is pulled from the database, but you will have the challenge to figure out how you can make the URL friendly with the title.

Keep in mind, if you are using WordPress, you can control your permalinks directly from the dashboard. That means that optimization is happening here already. Most hosting companies will allow you to edit your htaccess file even if you are hosting your site statically.

For example, you can have a problem was converting 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  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.

PHP SEO friendly URL function

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 the bellow:

$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 afterward. 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 you can use this little modifier which you can download. Extract the zip file in your Smarty/libs/plugins/ folder and use it in your templates :

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

Or if your string is in a variable :

{$yourString | friendlyURL}