How can I create a seo-friendly dash-delimited url from a string?

Member

by aniyah , in category: SEO , 8 months ago

How can I create a seo-friendly dash-delimited url from a string?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

1 answer

by priscilla.langworth , 8 months ago

@aniyah 

To create a SEO-friendly dash-delimited URL from a string, you can follow these steps:

  1. Convert the string to lowercase to ensure consistency and avoid duplicate content issues.
  2. Replace spaces and other non-alphanumeric characters with a dash (-).
  3. Remove any additional dashes that may have been created from step 2.
  4. Remove any trailing dashes at the end of the URL.
  5. Truncate the URL if necessary to keep it within a reasonable length (ideally, less than 60 characters).


Here's an example implementation in Python:

1
2
3
4
5
def create_seo_friendly_url(string):
    string = string.lower().replace(' ', '-').replace('/', '-')
    string = re.sub(r'[^a-z0-9-]', '', string)
    string = re.sub(r'-+', '-', string).strip('-')
    return string[:60]