Pretty URLs and the Trailing Slash

How should you decide when to use trailing slashes with pretty URLs? In my opinion you should never use them.

Why? Because a trailing slash denotes a directory, while the lack of it denotes a file/resource. So http://example.com/item/view/123 instead of http://example.com/item/view/123/. And certainly http://example.com/view?itemId=123 instead of http://example.com/view/?itemId=123. If these represented the directory structure of the web server it would be more straightforward, but with pretty URLs the content is dynamic and often can’t be bound to a directory structure.

This article explains the matter in more details. Note however that it advocates retaining the trailing slash, but it doesn’t focus on pretty urls in particular. Yes, you should retain the trailing slash for your index page and for any directories, but that’s it. The rest are resources and so they should not have the slash. The main point is that you must be consistent. From the practical point of view it is not that wrong to retain the slash (although it contradicts the directory structure logic), but have it for every resource, and have all your links conform to that choice.

What about SEO and 404 problems? With pretty urls the web server doesn’t handle redirects, so you have to handle them yourselves. You must accept both versions otherwise you may lose traffic and users might get 404s. But search engines treat the slash and the no-slash page as different resources and thus you may lose SEO “points”. So you must do 301 (Permanent) redirects from the slash to the no-slash url. How exactly depends on the framework and server you use.

1 thought on “Pretty URLs and the Trailing Slash”

Leave a Reply

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