Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

HTML

Aaron Banerjee
Aaron Banerjee
6,876 Points

How does the "/" automatically result in the root relative location of index.html?

In the relative roots video, we learned that using "/" can display index.html's page from another file. How does "/" automatically redirect the page to index.html if it is suppose to redirect the page to the root folder?

2 Answers

andren
andren
28,558 Points

It depends on what server you are using. But for Apache (the most common web server on the internet) when you request a directory rather than a file the server can be configured to look for files matching a certain name, and if one is found it will automatically serve that file even though its name was not specified.

The list of files Apache looks for is defined in its Directory Indexes directive, which out of the box includes index.html as the first file to look for. Therefore when Apache gets a request for a path (be that / or any other directory) it will always look for a file called index.html and serve it if found.

There are also servers (like Express for Node.js) where individual URLs are not represented by actual folders and files on the server, but actually configured entirely in code. In that case you can configure paths like / to return whatever data you want, including the index of your page.

Aaron Banerjee
Aaron Banerjee
6,876 Points

thanks for the great answer, also if i wrote " <img src = "/img/vr-user.jpg" alt ="A VR user">" does the "/img" mean exit whatever directory the current file is in, go to the root directory, and locate the img folder and the vr-user image pretty much?

andren
andren
28,558 Points

Yes, that is more or less accurate. There are three general types of paths you can use when linking to content. Relative paths, Root-relative paths, and absolute paths.

Relative paths (like img/abc.jpg) tells the browser to look for content from the same location as the file the link is placed in. So if the above link was placed in "example.org/about-us/index.html" then the browser would treat that link as "example.org/about-us/img/abc.jpg".

Root-relative paths (like /img/abc.jpg) tells the browser to look for content relative to the root of the server. So no matter where the file the link is placed in is located the browser will always look from the root of the server. So if the above link was placed in "example.org/about-us/index.html" then the browser would treat that link as "example.org/img/abc.jpg".

Absolute paths (like http://example.org/about-us/img/abc.jpg) tells the browser exactly where it should look for some content. With absolute paths the browser doesn't really have to think about what the root is or where the link is located as you are telling it exactly where to find the thing you are linking to.

Each of these methods has some up and downsides associated with them. But the most common of them in my experience is Root-relative paths.

Edit: Corrected mistake. Paths are resolved by the browser, not the server.