cPanel Hosting

Offer cPanel Hosting

Get Deal

How can I change the cPanel root directory?

At some you might want to customise or standardise your cPanel hosting account by changing the document root. This is important as the document root or the cPanel root directory is the default directory where website files are stored, and displayed when visitors access your site.

Why change the root directory in cPanel?

Changing the root directory for a domain in cPanel can help with:

  • Installing third-party apps that require custom directories for data.
  • Using a URL shortener
  • Testing or development of apps and different configurations
  • Creating a custom site structure.

Using an addon domain

1. Log into your cPanel account.

2. Navigate down to the Domains section and click on Addon Domain.

Addon domains cPanel
Addon domains

3. From the addon domain tool you can specific the Document Root location. And, press save when you have configured it correctly.

Create addon domain cPanel
Create addon domain

With Symlinks

1. Connect your account over SSH.

2. Backup the existing public_html file with tar -cvzf

tar -cvzf public_html_backup.tar.gz public_html

2. Create a symbolic link to the new directory from the current one using the command below, replacing the /path/to/directory with your new document root location. And, cpanel for your cPanel account username.

ln -s /path/to/directory /home/cpanel/

3. Finally test that this works, if not you may need to alter the file permissions with the chmod command.

Using .htaccess

You can use a redirect within .htaccess to redirect visitors to the correct document root.

1. From cPanel head to the file manager and open up the current document root location usually public_html.

cpanel file manager list
Directories

2. To add the redirect add the following lines into the .htaccess file replacing domain.tld with your domain and /path/to/directory with your new document root location.

RewriteEngine On RewriteCond %{HTTP_HOST} ^domain\.tld$ [NC] RewriteRule ^(.*)$ /path/to/directory/$1 [L]

How do I change the primary domain document root on cPanel?

It is not possible for non-root users to change the primary domain document root on a cPanel account without support (root) intervention, if you want to change the document root for the primary domain please create a support ticket.

Using SSH for root users

1. Connect to the server over SSH (with root access).

2. Using the text editor of your choice (we’ll be using nano) edit the following file replacing user with your the username for the account and domain.tld (the _SSL version) with the domain

nano /var/cpanel/userdata/user/domain.tld
nano /var/cpanel/userdata/user/domain.tld_SSL 

3. With nano open press the following keys simultaneously CTRL + W this will open the search window where you will now search: documentroot. This will take you to the document root line, now replace that with the new location and press CTRL + X to save your changes.

4. If there are any cache files present, remove them using a similar method as step two but with rm -vf.

rm -vf /var/cpanel/userdata/user/domain.tld.cache
rm -vf /var/cpanel/userdata/user/domain.tld_SSL.cache

5. To complete your changes you’ll need to update user data, rebuild the apache conf and restart httpd (apache). The command below chains together those necessary commands to be used at once.

/scripts/updateuserdatacache && /scripts/rebuildhttpdconf && service httpd restart