How to generate SEO-friendly URLs with PHP?

Member

by susan , in category: SEO , 10 months ago

How to generate SEO-friendly URLs with PHP?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

1 answer

by genevieve_boehm , 10 months ago

@susan 

To generate SEO-friendly URLs with PHP, you can follow these steps:

  1. Remove special characters, spaces, and replace them with hyphens
  2. Convert the URL to lowercase
  3. Trim repetitive hyphens and whitespaces
  4. Make sure the URL is unique and relevant to the content


Here is an example of a PHP function that generates SEO-friendly URLs:

1
2
3
4
5
6
function generate_seo_url($string){
    $string = preg_replace('/[^a-zA-Z0-9-_.]/', '-', $string);
    $string = strtolower(trim($string, '-'));
    $string = preg_replace('/-+/', '-', $string);
    return $string;
}


Note: This is just an example, you may need to modify the function to fit your specific needs.