Understanding Website Document Root Directory, Root Folder, Root Domain

Posted on: 05 Nov 2019 by Admin

When you visit a website, you are accessing a particular folder on a web server. For example, when you visit example.com, the server is pulling up the files at: /home/example/public_html. The web server only serves up the files located at that folder to incoming visitors. The location of that web folder is called the “document root”.
That specific location of the web folder is the document root and is set by the Apache configuration. But what if you wanted to change that location? cPanel’s default location may not serve your needs or you simply want to reorganize. In any case, I’ll show you how to make that change on cPanel.

Changing Add-on Domains

There are two types of domains on a cPanel box that can have document roots: Main (primary) domains and Addon domains. Changing the document root for Addon domains is easy. Simply log into your cPanel and navigate to: Domains >> Addon domains.

Next, edit the Addon domain path. To do so, simply click the edit icon next to the path, and type in your new path.

Changing Primary domains (Dedicated Server)

For changing the main/primary domain, you will need to have root SSH access and be able to locate and edit the following file (replacing your user & domain info):
/var/cpanel/userdata/USERNAME/DOMAIN.COM

1. Once you have opened the file, look for the following line:
documentroot: /home/USERNAME/public_html
2. Modify the location according to your needs. Save it and exit.
3. Rebuild the Apache conf and restart Apache:
/scripts/rebuildhttpdconf
service httpd restart
The change will be immediate. Simply clear your browser cache and force refresh the page!

Change document root using .htaccess

The public_html (most case) directory is called web root folder or document root folder. If you’ve created a test website under a sub-folder and you want it to be displayed when you type your domain name, add the following lines to the .htaccess file in the public_html folder:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain-name.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain-name.com$
RewriteCond %{REQUEST_URI} !folder-path/
RewriteRule (.*) / folder-path /$1 [L]
 In the above lines you should replace the following 2 strings:
domain-name.com – Type your own domain name folder-path – Type the name of the sub-folder which has the test/development website If you are running php website you may require below two lines also, RewriteCond %{HTTP_HOST} ^(www.)? domain-name.com$ RewriteRule ^(/)?$ folder-path /index.php [L]