You might have read about Search Engine Friendly URLs which are considered a backbone of SEO. A website’s URL Structure should be Clean and SEO Friendly and must not include Special Characters, because URLs with Special Characters are difficult to be remembered by a user. In this post I will discuss about some methods by which you can make a User Friendly & Search Engine Friendly URLs in your website.
The first and most important thing to keep in mind is, don’t create Urls with Special Characters like & ! @ # $ % ^ & * ( ). Search Engines will not encourage URLs having this kind of structure. Instead you should use URLs which contains only Alphabets and Numbers.
Secondly, you should use dashes as separators instead of spaces or underscores, as spaces are converted to %20 sign when indexed by Search Engines. Underscores are also not considered a best option as a separator because a text with underscores is considered a regular expression and they do not separate the words instead they make it one complete word.
Now let me tell you about How to Create an SEO Friendly and Clean Url with PHP. See the below code:
Making an SEO Friendly Url using PHP:
Following code will let you convert a simple text line into an SEO ready text for using in your url. Here we are using PHP language for removing unwanted special characters and repeated spaces and/or plus signs from our url:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
function clean_url($text) { $result_with_dashes = true; //set this to false if you want output with spaces as a separator $input_is_english_only = true; //set this to false if your input contains non english words $text = str_replace(array('"','+',"'"), array('',' ',''), urldecode($text)); if($input_is_english_only === true) { $text = preg_replace('/[~`\!\@\#\$\%\^\&\*\(\)\_\=\+\/\?\>\<\,\[\]\:\;\|\\\]/', " ", $text); } else { $text = preg_replace('/[^A-Za-z0-9\s\s+\.\)\(\{\}\-]/', " ", $text); } $bad_brackets = array("(", ")", "{", "}"); $text = str_replace($bad_brackets, " ", $text); $text = preg_replace('/\s+/', ' ', $text); $text = trim($text,' .-'); if($result_with_dashes === true) { $text = str_replace(' ','-',$text); } $text = preg_replace('/-+/', '-', $text); return strtolower($text); } |
Above is a PHP Function which will receive the text input from you and will return cleaned output which can further be used in your Url. Here is the usage of this function:
Usage of Clean Url Function:
This function is very simple to use. You just need to pass one parameter and it will process the whole string/text and will return output. You can save the output in a variable and use it where ever you want. See the example usage of this function below:
1 2 3 |
$raw_text = '-.fhj$%^&^jh^&jh$jh^jh<>?/;:\'"{}|hf-_=+)(*&^%$#@!~`<>.,;:[]{}/\ jhj'; $clean_text = clean_url($raw_text); |
Now $clean_text variable contains our cleaned text. We can now use it to make our url like this:
1 |
$url = 'http://example.com/2014/06/08/'.$clean_text; |
Complete Code:
Here i will show you complete usage of this function and creation of our URL:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
function clean_url($text) { $result_with_dashes = true; //set this to false if you want output with spaces as a separator $input_is_english_only = true; //set this to false if your input contains non english words $text = str_replace(array('"','+',"'"), array('',' ',''), urldecode($text)); if($input_is_english_only === true) { $text = preg_replace('/[~`\!\@\#\$\%\^\&\*\(\)\_\=\+\/\?\>\<\,\[\]\:\;\|\\\]/', " ", $text); } else { $text = preg_replace('/[^A-Za-z0-9\s\s+\.\)\(\{\}\-]/', " ", $text); } $bad_brackets = array("(", ")", "{", "}"); $text = str_replace($bad_brackets, " ", $text); $text = preg_replace('/\s+/', ' ', $text); $text = trim($text,' .-'); if($result_with_dashes === true) { $text = str_replace(' ','-',$text); } $text = preg_replace('/-+/', '-', $text); return strtolower($text); } /*after applying function to our string, we will get clean title stored in variable named clean_title*/ $url = 'http://example.com/2014/08/06/'.$clean_title; echo $url; /* Above will output a clean url which will be like this: http://example.com/2014/08/06/this-is-a-title-of-post $url variable has a clean and seo friendly url now you can use it anywhere you like :-) */ |
Feel free to download this script from here and use it anywhere in your code/projects.