Builds · Websites

Why does my page break without the trailing slash?

If a page works at yoursite.com/page/ but breaks at yoursite.com/page, it’s linking its files with relative paths. Without the slash, the browser looks for those files in the wrong folder, so the fix is to use paths that start with a slash and redirect the no-slash address to the right one.

One missing character. A completely broken page. Let me tell you how I found this one.

The page that worked every time, until it didn’t

One of my pages has a little calendar where couples check whether their date is open. I tested it over and over. It worked every time.

Then one day I opened it the way most people would, by typing the address from memory. The page loaded, but the calendar was just… gone. An empty space where it should have been. I chased a few theories (a stuck animation, a slow script) and fixed real things along the way, but the calendar still wouldn’t show.

The real answer was hiding in the address bar. Every test I’d run used a slash at the end. That day, I typed it without one.

The bug wasn’t in the code. It was in the address, one character long.

Why one slash changes everything

Let’s get a little nerdy for a moment. Web addresses work a lot like folders on your computer. When a page loads a file with a relative path, like calendar.js, the browser looks for it right next to the page.

But “next to the page” depends on whether the browser thinks the page is a folder or a file:

Same page, same code, different address, and the browser goes looking in the wrong place. The script never loads, so the calendar never draws. No error message on the page, nothing to tell you why. Just a hole.

The fix (it’s small, I promise)

  1. Use absolute paths for your files. Start every link to a script, stylesheet, or image with a slash and the full folder, like /vows/calendar.js. Now it resolves to the same file from any address.
  2. Redirect the no-slash address. Have your host send /vows to /vows/ automatically. Most hosts let you add this in a settings file or dashboard in a line or two.
  3. Pick one style for your whole site. Slash or no slash, choose one and redirect the other. One address per page keeps your analytics and search results clean too.
  4. Test the way real people type. Try every important page with and without the slash, and with capital letters. People are wonderfully unpredictable.

How to check your own site

Go to any page on your site that lives in its own folder, delete the slash at the end of the address, and press enter. If anything looks different (missing images, broken styling, a feature that doesn’t work), you’ve found it. If you use a website builder, you’re probably safe, since most handle this for you. It mostly bites hand-built pages.

The bottom line

Some bugs are loud. This one is a whisper: everything works while you test, because you always open the page the same way, and it breaks the moment someone types the address a little differently. Absolute paths and a single redirect make it impossible. Two small changes, and you never think about it again.

Is a page on your site acting strange for some visitors and not others? Let’s talk. And keep an eye on the blog for more honest notes from the things I build.

Common questions

Why does my website work with a trailing slash but not without it?
Your page links its files with relative paths. Without the slash, the browser treats the last part of the address as a file instead of a folder, so it looks for your scripts and images one level too high and cannot find them.
How do I fix relative path problems on my website?
Link your files with absolute paths that start with a slash, like /myfolder/script.js, so they resolve the same way from any address. Also redirect the no-slash version of the address to the slash version.
Should my URLs end with a slash?
Either style is fine if you pick one and redirect the other to it. Consistency matters more than the choice: one address per page keeps links, analytics, and search engines from splitting your page in two.
← All posts