This article explains how to alter the document root directory for a shared server account. The default document root directory for your account is /home/username/public_html, where username is your Hostginger Hosting account username. However, you may want to use a different directory as the document root on occasion. This is frequently the case, for example, if you want to test a new application or website setup before it goes live.

Use either of the following two procedures to change the document root directory.

The first method uses symbolic links and requires SSH access. The second method uses .htaccess redirection, and can be configured in cPanel.

Method 1: Making use of symbolic connections
The document root directory can be changed by copying the contents of the public_html directory to another directory and then creating a symbolic link that refers public_html to the new document root directory. Follow these steps to accomplish this:

  1. Log in to your account using SSH.
  2. At the command prompt, type the following command: mv ~/public_html ~/public_html_backup.
  3. Type the following command, replacing new_document_root with the name of the directory that you want to use as the new document root: ln -s ~/new_document_root ~/public_html
  4. To test your website, use a web browser. The browser should show material from the newly created document root directory.
Restoring the public_html directory:

You can revert to the original setup and utilize the public_html directory as the document root by reversing the technique described above. To accomplish this, take these steps:

  1. Log in to your account using SSH.
  2. At the command prompt, type the following: rm ~/public_html
  3. Replace source_directory with the name of the directory you want to use as public_html in the following command. This directory might be the public_html_backup directory you established in the last procedure, the new_document_root directory you were testing in, or something completely different: mv ~/source_directory ~/public_html
  4. To test your website, use a web browser. The browser should display content from the directory public_html.

Method 2: Using .htaccess redirection
To alter the document root directory, utilize redirection rules in an .htaccess file. You configure the web server to redirect incoming requests to a subfolder of your choice using this way. Follow these steps to accomplish this:

  1. Log in to cPanel.
  2. In the FILES section of the cPanel home screen, click File Manager.
  3. In the left sidebar, click the public_html folder. The folder's contents appear in the right pane.
  4. In the list of files, right-click .htaccess, click Edit, and then click Edit again.
  5. Copy and paste the text below into the .htaccess file. Substitute your own domain name for example.com, and replace subfolder with the name of the folder you want to utilize as the new document root directory. Furthermore, if you want to utilize a different primary file for your site than index.html, specify the filename (for example, index.php):
     # .htaccess main domain to subfolder redirect
     RewriteEngine on
  
     RewriteCond %{HTTP_HOST} ^(www.)?example.com$
     RewriteCond %{REQUEST_URI} !^/subfolder/

     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteCond %{REQUEST_FILENAME} !-d

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

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

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

6. Click Save Changes, and then click Close.

7. To test your website, use a web browser. The browser should show material from the newly created document root directory.

Restoring the public_html directory:
  1. Log in to cPanel.
  2. In the FILES section of the cPanel home screen, click File Manager.
  3. In the left sidebar, click the public_html folder.
  4. Right-click the .htaccess file in the file listing and select Rename.
  5. Type an other name for the .htaccess file (for example,.htaccess.bak) in the New file name text box, and then click Rename File.
  6. To test your website, open it in a web browser. The browser should now see items from the public_html directory.
Was this answer helpful? 0 Users Found This Useful (0 Votes) apache web server