Consider the first scenario :
If I type in the URL https://www.w3schools.com/html
into the address bar of my web browser it automatically changes to the URL https://www.w3schools.com/html/
which has a trailing slash and then the relevant web page gets loaded.
Why and how the trailing slash is getting added to the URL I entered into my web browsers address bar? Who is responsible for automatically adding the trailing slash to the URL I entered earlier?
Consider the second scenario :
Consider below anchor tag <a>
code :
<a href="https://www.w3schools.com/html/">Visit our HTML tutorial</a>
This works fine. Then, I removed the trailing slash from the same URL and tried below anchor tag <a>
code which is not working :
<a href="https://www.w3schools.com/html">Visit our HTML tutorial</a>
Why so?
Whys it's not getting redirected to the URL mentioned into src
attribute of the anchor tag <a>
?
Although, I don't know the reason behind the automatic addition of trailing slash to the URL entered into web browser's address bar my question is why the same funda is not working out in the second scenario i.e. in anchor tag <a>
that contains an URL in its href
attribute which doesn't contain trailing slash?
Please someone help me by answering all of my doubts in simple, lucid and easy to understand language.
Thank You.
These two urls:
Are canonically different urls. This means that a server and a client should treat them as 2 distinct urls. No client should automatically add or remove the slash, because they might return very different things.
However, many servers and server frameworks will automatically add the slash. They do this via a HTTP redirect.
If you run plain apache for example, and you serve index.html
from some kind of directory, by default apache will redirect to the url with a slash if none was provided.
This is not required by HTTP, or required for servers or clients to work. This is strictly a choice by the implementer of the server.