Change Primary Domain Document Root in cPanel/WHM

The cPanel accounts are created using a primary domain. All primary domains on the hosting account use “public_html” directory for all its website files and data. The sub-directories inside the public_html directory is occupied by the addon domains. The primary domain can also be setup to use a sub-directory inside public_html directory instead of public_html itself. Follow the below steps to change the document root of your primary domain in cPanel account. Please note that, you will need to have root SSH access to perform these steps.

1) Connect to your server via SSH as root user. You may follow the tutorial below, if you are using a Windows system to connect to your server via SSH. This tutorial explains how to use “Putty”, SSH client software to access server.

https://www.1onlyhost.net/tips/kb/using-putty-to-ssh-to-server/

2) Using your favorite text editor (say vim) edit the following file.

$ vim /cpanel/userdata/username/domain.com

Replace the “username” with your cPanel account username and “domain.com” with your primary domain name and “subdir” with your new directory. Find the following two lines in this file.

documentroot: /home/username/public_html

path: /home/username/public_html/cgi-bin

Modify these two lines to change the document root of your primary domain to a sub-directory inside “public_html” directory.

documentroot: /home/username/public_html/subdir

path: /home/username/public_html/subdir/cgi-bin

Save the file after changes are made and then delete the cache file for your primary domain.

$ rm -vf /var/cpanel/userdata/username/domain.com.cache

3) If the primary domain has an SSL certificate installed, edit the following file the same way as above.

$ vim /var/cpanel/userdata/username/domain.com_SSL

Save the file after changes are made and then delete the cache file for your primary domain.

$ rm -vf /var/cpanel/userdata/username/domain.com_SSL.cache

4) Run the following scripts to update the user data cache and rebuild apache configuration file.

/scripts/updateuserdatacache

/scripts/rebuildhttpdconf

5) Restart Apache server to load changes.

$ service httpd restart

 

Redirection using .htaccess file

You can also set up a redirection in the .htaccess file inside the public_html directory. This will tell the Apache server to redirect any request for your primary domain to a sub-directory on public_html. Please follow the below steps to open .htaccess file for editing.

1) Log in to your cPanel account.

2) Scroll down to the Files section.

3) Click the ‘File Manager’ icon.

4) Check the View Hidden Files checkbox.

5) You are working with your primary domain, so select the ‘WebRoot (public_html/www)’ option.

6) Right click the ‘.htaccess’ and choose Code Editor.

It is recommended to follow the instructions provided in the official documentation if you are using CMS like WordPress, Joomla, Drupal for your website. Please see the below links for specific instructions.

https://codex.wordpress.org/Giving_WordPress_Its_Own_Directory

https://docs.joomla.org/Moving_the_site_among_directories/sub-directories

https://www.drupal.org/node/144643

 

You need to add the following code to the .htaccess file in the public_html directory and make modifications as noted in the (#) comments. Replace the two instances of “example.com” with your primary domain and three instances of “subdirectory” to the directory where you want your site.

# .htaccess main domain to subdirectory redirect

# Do not change this line.

RewriteEngine on

# Change example.com to be your main domain.

RewriteCond %{HTTP_HOST} ^(www.)?example.com$

# Change ‘subdirectory’ to be the directory you will use for your main domain.

RewriteCond %{REQUEST_URI} !^/subdirectory/

# Don’t change the following two lines.

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

# Change ‘subdirectory’ to be the directory you will use for your main domain.

RewriteRule ^(.*)$ /subdirectory/$1

# Change example.com to be your main domain again.

# Change ‘subdirectory’ to be the directory you will use for your main domain

# followed by / then the main file for your site, index.php, index.html, etc.

RewriteCond %{HTTP_HOST} ^(www.)?example.com$

RewriteRule ^(/)?$ subdirectory/index.html [L]

I hope you have found this article useful. If you need any further assistance, please contact our support department.

If you need any further help please reach our support department

Was this answer helpful? 0 Users Found This Useful (0 Votes)