Skip to content Skip to sidebar Skip to footer

HTML Link Trailing Slash

The w3schools documentation says: Without a trailing slash on subfolder addresses, you might generate two requests to the server. Many servers will automatically add a trailing sl

Solution 1:

These are two different URLs:

http://example.com/foo
http://example.com/foo/

Often, but not always, requesting the first URL will trigger the server to reply with a 301 Permanent Redirect to the second URL. The browser will then have to make a second request to the second URL.

This is most commonly the case when the URL is mapped on to a directory on the server's file system and the index.html (or other directory index) is being loaded.

Servers where the content is being dynamically generated (e.g. with an MVC framework like Perl's Catalyst) are less likely to do this. In that case you often have to be even more careful with where you link to because relative URLs will resolve differently from the two URLs.


Solution 2:

Fundamentally, http://example.com/foo and http://example.com/foo/ are two entirely different URLs. Ultimately what's important is how the server serving those URLs will respond when queried for those URLs. And it's entirely up to the server what to do. .../foo may return a file while .../foo/ may return a directory listing. Or both may return a directory listing. Or a file. Or the same file. Or a random new response each and every time.

What W3S is pointing out is that many servers are by default configured to return a redirect response to the canonical version ending in a slash. Meaning, if you're requesting .../foo from that server, it will redirect you to .../foo/, which then causes your client to do a second request to .../foo/. Why or how or when a server may issue this redirect is entirely up to each server, and whether it's really such a popular practice is questionable (as is everything by W3S).

The important thing is that you point your URLs where you mean to point them. Is .../foo the correct URL because it's a file? Or is .../foo/ the correct URL because it's the root of a (virtual) directory? You decide, you make sure your server behaves appropriately.


Post a Comment for "HTML Link Trailing Slash"