Document Root – Absolute Path

Published on June 19, 2022 at 6:41 pm by LEW

Introduction

When using a web hosting provider, or even setting up your own web server, you may notice that not all absolute paths are the same. This is because of the web server document root.

Relative paths are great, and short. But there are always some use cases where you need the absolute path. When dealing with web hosting services, there can appear to be multiple absolute paths. In reality there is only one, and it is the web server itself that can create the appearance of multiple absolute paths.

Absolute Path vs Relative Path

I have discussed the difference between reactive and absolute paths before. But there is nothing wrong with covering the subject again.

Everything in Linux is treated as a file, and is located somewhere on the Linux directory tree. Information on the Linux File System Hierarchy can be found here. And while this is useful at a higher level, when you get into specific directories, you will find Linux distributions (Distros) all like do things a little differently from each other.

Navigating the file tree can be done either with a full path that goes all the way to the root of the tree or by using relative paths which map directly from one directory straight to another using “..” and “.” to move up and down the tree structure.

As an example, I want to copy a picture file from its storage folder to a working project folder located within a home directory. I am in the working directory, located at /home/user/project/wallpaper. The first example uses absolute paths. The second example uses a relative path

cp /home/user/pictures/2016_03_23/hotsprings.jpg \
/home/user/project/wallpaper/candidate03.jpg

cp ../../pictures/2016_03_23/hotsprings.jpg candiate03.jpg

Using relative paths is a bit shorter on the typing. Each instance of “..” moves you up one directory. And since we are already in the working directory, there is no need for its path.

Absolute Paths Always Start at Root

The top of the Linux file tree is represented by the forward slash “/”. All absolute paths must start there. Where you can go will depend on file permissions. On a web host site, permissions will most likely limit you to your user home folder (if they are security conscious). So technically all your usable absolute paths would start with “/home/username”.

Depending on how the hosting company has set up share hosting, there should be additional folders. Specifically domains and public_html. The domains directory is really only relevant if your provider lets you host multiple domains. So the absolute path to your web site (assuming no sub folders) may be something like this.

/home/username/domains/public_html

Web Server Document Root

When referencing files form inside your html, you will need to use the document root. This is something that is defined in the web server configuration file for your website. It is most likely the same as the above example. However if you are running multiple web sites on the same share, it could include additional directory depth. A few examples should illustrate this point.

You are running a single web site at the root level, your document root would be as follows.

/home/username/domains/public_html

If you are running both a wiki and a cloud server as separate instances, in different folders, you might have two document roots, one for each hosting instance.

/home/username/domains/public_html/wiki

/home/username/domains/public_html/cloud

When to Use What

When you are working with HTML and CSS, you will want to use then document root defined by the web server configuration files.

For anything else, you will need to use the absolute path.

Conclusion

In this post we covered absolute paths, relative paths, and web server document root. If you are getting into website coding of any sort, you will need to know and understand the differences.

 

Add New Comment

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