2 April, 2015

Making a Blog URL, SEO Friendly With PHP
By: Matthew Jackson

The key to having a SEO friendly url is pretty simple. You want the title, which describes the article to be the url. Atleast that is the simplest way to do it.... It is much better than the id value of your blog.

Either way you want to format the text as follows:

function getSeoUrl($string) {
    //Lower case everything
    $string = strtolower($string);
    //Make alphanumeric (removes all other characters)
    $string = preg_replace("/[^a-z0-9_\s-]/", "", $string);
    //Clean up multiple dashes or whitespaces
    $string = preg_replace("/[\s-]+/", " ", $string);
    //Convert whitespaces and underscore to dash
    $string = preg_replace("/[\s_]/", "-", $string);
    return $string;

The key here is to have a special column for your blog to not just be a title, but also the SEO url. That way the url is formatted once. The space is minimal and you can quickly search the database for the SEO url version without converting each title every time you search.

Why not use the title? Well titles first of all usually have spaces which are not friendly in the url, but they might have many illegal characters that are definitely not friendly.

Tags: Blog, PHP, SEO