📚 General & Other

Trailing Slash Rules Have More Edge Cases Than You Think

The naive rule — if the line does not end in , append one — is wrong for several common URL shapes, and each wrong case produces a URL that looks fine and resolves to nothing.

The naive rule — if the line does not end in /, append one — is wrong for several common URL shapes, and each wrong case produces a URL that looks fine and resolves to nothing.

Do not slash a file

/downloads/report.pdf/ is not the PDF. Any URL whose final path segment contains a dot is probably a file, and appending a slash to it produces a 404. The rule needs to skip those.

Query strings and fragments are not the path

The slash belongs at the end of the path, not the end of the string. /search?q=shoes must become /search/?q=shoes, never /search?q=shoes/ — the second appends a slash to the value of q, silently changing the query. Same for #anchor.

The root is already correct

https://example.com normalises to https://example.com/, and that one is genuinely just an append. But a rule that treats the whole URL as an opaque string cannot tell that case apart from the three above.

Why pick the tool over the regex

A correct implementation parses the URL, modifies the path component, and reassembles it. That is a real piece of code, not a one-liner, and it is worth not rewriting it under time pressure.

Try it: Add Trailing Slash to URLs on SeoWolf's Notepad.