DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowHalloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
FREEDOM251

Freedom251 › Blog › JavaScript

How to Remove Emojis and Special Characters from Website URLs Safely

By Freedom251 Team · · 11 min read

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Remove emojis and unwanted punctuation from the URL path—not from the complete URL string. First parse the address into its scheme, host, path, query, and fragment. Then clean only the path segments your application controls, preserve delimiter characters such as /, ?, #, &, and = where they have URL syntax meaning, and redirect any old published path to the new canonical address.

Use deletion or transliteration when you are deliberately creating a readable slug. Use UTF-8 percent-encoding when the original text must remain faithfully recoverable. Those are different jobs, and choosing the wrong one can cause broken routing, duplicate pages, lost distinctions, or changed query behavior.

As an Amazon Associate I earn from qualifying purchases.

What you are actually cleaning

A URL is structured data, not one undifferentiated string. RFC 3986 divides it into components with different rules:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Component Typical example Cleaning rule
Scheme https Do not alter it while cleaning a slug.
Authority example.com:443 Do not run a slug filter over the host, port, user information, or credentials.
Path /news/😊-launch/ Clean individual segments when your application owns the route.
Query ?q=😊&page=2 Leave it alone unless you are intentionally normalizing a specific parameter; query delimiters carry meaning.
Fragment #comments-2 Usually leave it unchanged; browsers use it for in-page navigation and it is not sent to the server.

The RFC 3986 unreserved set consists of letters, digits, hyphen (-), period (.), underscore (_), and tilde (~). Characters such as /, ?, #, &, and = are reserved because they separate URL components or parameters. Removing them globally can make a different resource request.

Choose a policy before writing a cleaner

There is no universally correct definition of “special character.” Decide whether the path is a human-readable identifier or a transport for data.

Policy What happens Advantages Costs and risks
Strict ASCII slug Normalize text, remove emoji and disallowed characters, replace separators with hyphens, and lowercase when appropriate. Readable, predictable, and broadly compatible with routers and tools. Accents, non-Latin scripts, and distinctions between similar titles can be lost; collisions require unique IDs or suffixes.
Unicode slug Keep letters from the original language while removing only characters your policy rejects. Preserves international-language meaning and can remain readable to local audiences. Different normalization forms can produce visually identical but byte-different paths; integrations and logs may be less consistent.
Percent-encoded component Keep the data and encode bytes outside the permitted URL set as UTF-8 percent escapes. Reversible and faithful to the original value. Less pleasant to read, and encoding the wrong delimiter can change how a router parses the value.

Evaluate a policy on six questions: readability, reversibility, collision risk, support for the languages you publish, compatibility with your router, and how you will canonicalize and redirect existing addresses. A strict ASCII slug favors interoperability; percent-encoding favors preservation.

Build a safe slug-cleaning pipeline

Apply the operations in a fixed order. Changing the order can leave combining marks, produce duplicate separators, or decode a delimiter into syntax.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  1. Normalize Unicode. Choose NFKC or NFKD according to your site’s policy. NFKD is useful when an ASCII-only result is required because it separates many letters from their combining marks. NFKC performs compatibility folding without automatically making the result ASCII.
  2. Remove emoji and disallowed symbols. Emoji are often sequences, not one visible character. A sequence can include a base code point, a variation selector, skin-tone modifier, regional indicators, and a zero-width joiner (ZWJ) connecting additional characters. Remove the sequence’s participating code points, not just one narrow range.
  3. Transliterate or remove combining marks. For ASCII-only slugs, transliterate where your product policy supports it, or drop combining marks after NFKD. If you publish in multiple languages, retaining native letters may be more respectful and less collision-prone than transliteration.
  4. Convert separators to one hyphen. Treat runs of whitespace and existing hyphens as one separator. Decide whether underscores are meaningful in your router; they are unreserved, but many editorial systems prefer hyphens for readability.
  5. Collapse and trim. Collapse repeated hyphens and remove leading and trailing hyphens. Do not create an empty path segment accidentally when a title contains only removed characters.
  6. Apply case policy. Lowercase only if the site treats paths case-insensitively or has a canonical lowercase convention. Some servers and storage systems distinguish uppercase and lowercase paths.
  7. Handle an empty result. If everything was removed, use a stable identifier such as an internal numeric or opaque ID. Do not invent a random slug on every request; that creates unstable links.
  8. Check uniqueness. Two titles can normalize to the same slug. Reserve the slug in a database and add a deterministic suffix or ID when a collision occurs.
  9. Redirect old paths. When a published URL changes, return a permanent redirect from the old path to the canonical path, update internal links and XML sitemaps, and preserve query parameters when they are still meaningful.

Example transformations

Input segment ASCII-slug result Reason
😊 Summer  Sale!!! summer-sale Emoji and punctuation are removed; whitespace is normalized.
Café déjà vu cafe-deja-vu NFKD separates accents and combining marks are removed.
مرحبا بالعالم not stated The result depends on whether the site transliterates Arabic or permits Unicode slugs; choose and document one policy.
--- fallback identifier A slug made only of separators becomes empty after trimming.

Python: clean path segments without damaging the URL

The standard-library approach is to parse first, clean each path segment, and rebuild the URL while retaining the original query and fragment. The following example is intentionally ASCII-oriented.

from urllib.parse import urlsplit, urlunsplit
import re
import unicodedata

EMOJI_OR_SYMBOLS = re.compile(r'[\U0001F000-\U001FAFFF\u200d\ufe0f]')
NOT_SLUG = re.compile(r'[^a-zA-Z0-9\s-]')


def clean_slug(text: str) -> str:
    text = unicodedata.normalize('NFKD', text)
    text = EMOJI_OR_SYMBOLS.sub('', text)
    text = ''.join(ch for ch in text if not unicodedata.combining(ch))
    text = NOT_SLUG.sub('', text)
    text = re.sub(r'[-\s]+', '-', text).strip('-')
    return text.lower()


def clean_url_path(url: str) -> str:
    parts = urlsplit(url)
    segments = [clean_slug(segment) for segment in parts.path.split('/')]
    new_path = '/'.join(segments)
    return urlunsplit((parts.scheme, parts.netloc, new_path,
                       parts.query, parts.fragment))

For example, a path such as /guides/😊-Café!!! becomes /guides/cafe, while an existing query such as ?ref=summer&page=2 and fragment such as #details remain untouched. The function does not decide whether a trailing slash is canonical; configure that rule separately in your router.

The regular expression is a practical filter, not a complete Unicode grapheme parser. New emoji can be added outside a hard-coded range, and complex sequences can include code points that need separate handling. For a production multilingual system, use a maintained Unicode-aware library or an explicit allow-list policy, and test the versions you deploy.

When to use Python quoting instead

Use urllib.parse.quote() when the value is data that must survive transport rather than a title that should be rewritten. Path quoting and form/query encoding are different operations: quote() is designed for URL components, while quote_plus() is aimed at form-style query data and represents spaces as plus signs. Do not use quote_plus() to build a path unless that is explicitly your server’s convention.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

JavaScript: operate on pathname, not the whole address

In browser or server-side JavaScript, construct a URL, split the pathname into segments, clean only the segments, then assign the result back. Decode only a component that you have already isolated.

function cleanSlug(value) {
  return value
    .normalize('NFKD')
    .replace(/[\u{1F000}-\u{1FAFFF}\u200D\uFE0F]/gu, '')
    .normalize('NFD')
    .replace(/[\u0300-\u036f]/g, '')
    .replace(/[^a-zA-Z0-9\s-]/g, '')
    .replace(/[\s-]+/g, '-')
    .replace(/^-+|-+$/g, '')
    .toLowerCase();
}

function cleanUrlPath(input) {
  const url = new URL(input);
  const segments = url.pathname.split('/').map(segment => {
    const decoded = decodeURIComponent(segment);
    return cleanSlug(decoded);
  });
  url.pathname = segments.join('/');
  return url.toString();
}

new URL() keeps the query and fragment separate from pathname. A global regular expression over the original string can remove /, ?, #, or &, changing routing and parameter boundaries. Decode only after isolating a segment, clean it, and let the URL object serialize it again.

If malformed percent escapes are possible, wrap decodeURIComponent() in error handling and reject or log the invalid request rather than guessing. Also decide how to treat an encoded slash (%2F): some routers decode it before route matching, while others preserve it, so a slug cleaner cannot make that behavior consistent by itself.

Percent-encode when deletion would lose meaning

Suppose a product name, search term, document key, or user-generated identifier must be recoverable exactly. Deleting emoji or punctuation is then destructive. Keep the Unicode text and percent-encode the UTF-8 bytes that are not allowed in that component. A percent-encoded octet represents one byte; it is not a second textual character.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Encode the value at the component boundary, not after concatenating an entire URL.
  • Keep delimiters that belong to the URL grammar outside the encoded data.
  • Encode a literal reserved character when it is data. For example, a literal ampersand inside a query value must be encoded so it cannot be mistaken for the next parameter.
  • Decode once, in the component that owns the value. Repeated decoding can turn data into delimiters and create security or routing problems.

In JavaScript, use encodeURIComponent() for a data component. In Python, use urllib.parse.quote() for a path component and choose the safe characters deliberately. Neither function should be applied indiscriminately to a complete URL.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Protect routing, queries, and fragments

Path separators

A slash separates path segments. If your title contains a literal slash and the title is one slug, encode that slash as data or replace it according to policy; do not leave it unexamined, because it may create an extra route segment.

Query delimiters

The question mark starts the query, ampersands separate parameters, and equals separates a parameter name from its value. A cleaner that strips or replaces these characters across the whole URL can turn several parameters into one, or move text from the path into the query. Parse the query into key-value pairs if you need to normalize one parameter, then serialize it with a component-aware encoder.

Fragments

The fragment begins with a hash and is interpreted by the client. It is not sent in an HTTP request, so server-side redirects cannot inspect it. Preserve it when rewriting a browser URL unless your client application intentionally maps it to a new anchor.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Encoded delimiters

Do not decode an entire URL before parsing. A percent-encoded question mark or slash may be data inside a segment. Parse the raw structure first, then decode the isolated component, validate it, and encode it again when rebuilding the address.

Redirect and canonicalization rollout

  1. Record the old-to-new mapping. Store the previous path and canonical path, including any locale or version prefix that is part of routing.
  2. Return a permanent redirect. Use the redirect status your deployment standardizes for permanent moves, and preserve query parameters that still have meaning.
  3. Update first-party references. Change navigation, feeds, sitemaps, structured data, documentation, and API examples to the canonical form.
  4. Keep the old route available. Do not delete it immediately; redirect chains and stale bookmarks can persist for years.
  5. Monitor collisions and loops. Log requests to old paths, repeated redirects, and cases where two old paths map to one new slug unexpectedly.

Canonicalization must be deterministic. If one request produces summer-sale and another produces summer-sale-, caches and search engines can treat them as different resources. Apply the same normalization at creation, lookup, redirect, and sitemap generation.

Common failure modes and fixes

Symptom Likely cause Fix
The entire URL becomes one path. A regex removed ?, #, or slashes globally. Parse with a URL library and clean only path segments.
Emoji remain as an invisible box or variation mark. The filter removed the base code point but left variation selectors or ZWJ characters. Handle the complete sequence or use a Unicode-aware policy; test rendered and code-point forms.
Accented letters disappear unpredictably. Normalization and combining-mark removal are inconsistent. Choose NFKC or NFKD deliberately and apply the same policy everywhere.
Two articles receive the same slug. Different titles normalize to one ASCII result. Reserve slugs and append a stable identifier on collision.
A title becomes an empty string. All characters were disallowed or were separators. Use a stable fallback ID and show a validation error when a human slug is required.
Parameters stop working after rewriting. Query text was cleaned as if it were a path. Parse the query separately and encode each value, preserving its delimiters.
Uppercase and lowercase URLs behave differently. The origin server or router is case-sensitive. Do not lowercase unless the site’s routing policy guarantees case-insensitive paths; otherwise redirect only known variants.
Redirects loop. The old-path rule and canonical-path rule rewrite each other. Compare normalized values before redirecting and stop when the request already equals the canonical path.

Security and validation checks

  • Reject control characters, unexpected line breaks, and null bytes before logging or forwarding a URL.
  • Validate after decoding, because a dangerous character can be hidden inside a percent escape.
  • Decode once only; repeated decoding can manufacture slashes, query delimiters, or control characters.
  • Do not allow a user-controlled host, scheme, or authority to be replaced by a slug-cleaning routine.
  • Apply length limits to segments and the complete URL appropriate to your server, proxy, database, and browser stack.
  • Keep routing policy separate from display text. A title shown on a page can contain characters that the canonical path intentionally omits.

Test the cleaner before changing published URLs

Use automated tests plus real router integration tests. At minimum, include:

  • Plain ASCII words, repeated spaces, repeated hyphens, leading separators, and trailing separators.
  • Accented Latin text in both precomposed and decomposed Unicode forms.
  • Emoji with variation selectors, skin tones, flags, and ZWJ sequences.
  • Non-Latin titles and the site’s chosen transliteration or Unicode behavior.
  • Strings containing literal slash, question mark, hash, ampersand, equals, percent, and plus signs.
  • Already percent-encoded input, malformed percent escapes, and encoded delimiters.
  • A URL with a port, query parameters, and a fragment to verify that only the intended component changes.
  • Titles that normalize to the same slug, titles that normalize to nothing, and paths with or without a trailing slash.
  • Case variants and repeated application of the cleaner, confirming that a second pass produces the same result.

For every test, verify both the serialized URL and the server’s route match. A string that looks correct in a browser bar can still map to a different path, parameter set, or resource.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A practical decision rule

If the path is an editorial label and readers benefit from a short, stable address, use the slug pipeline: normalize, remove or transliterate according to policy, hyphenate, validate uniqueness, and redirect old paths. If the path carries an identifier whose exact characters matter, keep the value and percent-encode its component. In either case, parse first, preserve URL syntax, validate after decoding, and make canonicalization deterministic.

Quick Recap

SaleBestseller No. 1

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

JavaScriptPythonSEOUnicodeURL slugsURLsWeb development

Leave a Reply

Your email address will not be published. Required fields are marked *

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.