create seo friendly url

How to create SEO friendly url in PHP

This post has been viewed 153 times.

In this tutorial we would be creating a PHP function that will convert a string to create SEO friendly url that can be used in your website.

We will also use some predefined PHP functions such as strtolower and preg_replace for this tutorial. The goal of this tutorial is to create SEO friendly url from string.

You may see that the url of this tutorial is http://www.davidangulo.xyz/website-development/how-to-create-seo-friendly-url-in-php/.

From that, we can observe the characteristics of a SEO friendly url. First thing is that the url is only composed of lowercase characters, no special characters, no whitespaces and last no “%20” in the url.

What you’ll need:

  • Basic PHP functions knowledge.

Step 1: Create a function with a string parameter.

We would be using the parameter $string to receive the data when our function is being invoked.

Step2: Convert the string to lowercase.

This will now return a lowercase version of the string.

Step 3: Remove special characters from the string.

We added a line of code that will remove all special characters leaving only the alphanumeric present.

Step 4: Remove all double dashes “–” and double spaces ”  “

The next line of code will remove duplicates from spaces and dashes.

Step 5: Convert all underscores and white spaces to dashes.

Complete Code:


The last line of code that we added converts all underscore and spaces to dashes creating a SEO friendly url. This will be the last step and this function will now return an SEO friendly url.

Basic usage:

That’s pretty much it for this tutorial. I hope you have a good use for it.

Hello, I am a freelance website and mobile application developer. I am also the owner of this website, so if you have any projects for me you can contact me at [email protected] or use the contact page. When I’m not doing some work, I can be found watching Anime/Asian Dramas/American TV Series, playing computer games, and writing tutorials on this website. I run the program repeatedly hoping the error won’t show up next time.