How to Create a URL Slug from a Title

Published April 2025 · 4 min read

A good URL slug is lowercase, uses hyphens, and contains only letters and numbers.

JavaScript

function slugify(text) {
  return text.toLowerCase()
    .normalize('NFD').replace(/[\u0300-\u036f]/g, '')
    .replace(/[^a-z0-9\s-]/g, '')
    .trim().replace(/[\s-]+/g, '-');
}
slugify("How to Fix JSON Errors!"); // "how-to-fix-json-errors"

Python

import re, unicodedata
def slugify(text):
    text = unicodedata.normalize('NFD', text).encode('ascii', 'ignore').decode()
    text = re.sub(r'[^a-z0-9\s-]', '', text.lower())
    return re.sub(r'[\s-]+', '-', text).strip('-')

slugify("Héllo Wörld!") # "hello-world"

PHP

function slugify($text) {
    $text = iconv('UTF-8', 'ASCII//TRANSLIT', $text);
    $text = preg_replace('/[^a-z0-9\s-]/', '', strtolower($text));
    return preg_replace('/[\s-]+/', '-', trim($text));
}

No Code?

Use our Slug Generator — paste any title, get a clean slug.

Related