> ## Documentation Index
> Fetch the complete documentation index at: https://kb.hosting.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Changing the document root directory

> Learn how to change the default document root directory on a shared server from public_HTML with these easy steps.

This article describes how to change the document root directory for an account on a shared server. By default, your account's document root directory is the */home/username/public\_html* directory, where ***username*** represents your hosting.com account username. However, sometimes you may want to use a different directory as the document root. This is often the case, for example, if you want to try out a new application or test a website configuration 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.

<Note>
  If you want to change the document root directory for an addon domain, do not follow the procedures below. Instead, use cPanel to change the addon domain's document root directory. For more information about how to do this, please see [this article](/docs/cpanel-domains-tool).
</Note>

## Method #1: Use symbolic links

You can change the document root directory by moving the contents of the **public\_html** directory to another directory, and then creating a symbolic link that points **public\_html** to the new document root directory. To do this, follow these steps:

1. Log in to your account [using SSH](/docs/using-ssh-secure-shell).

2. At the command prompt, type the following command:

   ```bash theme={null}
   mv ~/public_html ~/public_html_backup
   ```

   > 🚧 Important
   >
   > * This command moves the **public\_html** directory, and all of the files it contains, to the **public\_html\_backup** directory. You can use another name besides **public\_html\_backup** if you want.
   >
   > * After you run this command, you must run the next command in step 3 as soon as possible for two reasons. First, your site is inaccessible as long as **public\_html** is missing. Second, cPanel will automatically recreate the **public\_html** directory if it detects that it is missing. If you have not run the command in step 3 before this occurs, your site will not function like you expect.

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:

   ```bash theme={null}
   ln -s ~/new_document_root ~/public_html
   ```

   > 🚧 Important
   >
   > Your site may be inaccessible until the server's cron job sets the correct file permissions and group ownership for the new document root directory. This cron job runs on the server at twelve minutes past the hour on every hour evenly divisible by four (4:12, 8:12, 12:12, etc.). To minimize any possible downtime for your site, you can change the document root directory just before one of these scheduled times. (To determine the current time on the server, access a terminal [using SSH](/docs/using-ssh-secure-shell) or by [logging in to cPanel](/docs/accessing-cpanel), and then in the **Advanced** section, click **Terminal**. At the command prompt, type `date` and then press Enter.) After the cron job runs, the new document root directory file permissions are set to **750**, and the group ownership is set to **nobody**, as shown in the following sample directory listing:
   >
   > ```
   > drwxr-x---  3 username nobody       4096 Mar 10 10:08 new_document_root/
   > ```

4. Use a web browser to test your web site. The browser should display content from the new document root directory.

   > 👍 Tip
   >
   > If your web site does not function correctly:
   >
   > * Make sure that the new document root directory exists and contains web site files (such as *index.html* or *index.php* ).
   >
   > * Make sure you run the commands in step 2 and 3 in quick succession. To verify that the symbolic link is correct, type the following command:
   >
   >   ```bash theme={null}
   >   ls -l ~/public_html
   >   ```
   >
   >   You should see output similar to the following, where ***username*** represents your account username:
   >
   >   ```
   >   /home/username/public_html -> /home/username/new_document_root
   >   ```
   >
   >   If you do not see similar output, then the symbolic link is not set up correctly. Type the following command:
   >
   >   ```bash theme={null}
   >   rm -rf ~/public_html
   >   ```
   >
   >   Then repeat steps 2 and 3.

### Restoring the public\_html directory

You can restore the original configuration and use the **public\_html** directory as the document root again by reversing the procedure that you followed above. To do this, follow these steps:

1. Log in to your account [using SSH](/docs/using-ssh-secure-shell).

2. At the command prompt, type the following:

   ```bash theme={null}
   rm ~/public_html
   ```

   > 🚧 Important
   >
   > * This command removes the symbolic link that redirects **public\_html** to the modified document root directory.
   >
   > * After you run this command, you must run the next command in step 3 as soon as possible for two reasons. First, your site is inaccessible as long as **public\_html** is missing. Second, cPanel will automatically recreate the **public\_html** directory if it detects that it is missing. If you have not run the command in step 3 before this occurs, your site will not function like you expect.

3. Type the following command, replacing ***source\_directory*** with the name of the directory that you want to use as **public\_html**. For example, this directory could be the **public\_html\_backup** directory that you created in the previous procedure, the **new\_document\_root** directory that you were using for testing, or an entirely different directory:

   ```bash theme={null}
   mv ~/source_directory ~/public_html
   ```

4. Use a web browser to test your web site. The browser should display content from the **public\_html** directory.

## Method #2: Use redirection with .htaccess

This method uses redirection rules in an *.htaccess* file to change the document root directory. With this method, you configure the web server to redirect incoming requests to a subfolder of your choice. To do this, follow these steps:

1. Log in to cPanel.
   > 📘 Note
   >
   > If you do not know how to log in to your cPanel account, please see [this article](/docs/accessing-cpanel).

2. In the **FILES** section of the cPanel home screen, click **File Manager**:\
   ![cPanel - File Manager icon](https://static.hosting.com/kb/kb-cpanel-78-file-manager-icon.png)

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.

   > 📘 Note
   >
   > If the *.htaccess* file is not in the list of files, you must create it first. On the top menu bar, click **+    File** . In the **New File Name** text box, type **.htaccess** , and then click **Create New File** .

5. Copy and paste the following text into the *.htaccess* file. Replace *example.com* with your own domain name, and replace ***subfolder*** with the name of the folder that you want to use as the new document root directory. Additionally, if you want to use a different main file for your site instead of *index.html*, specify the filename you want to use (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]
   ```

   > 📘 Note
   >
   > The subfolder you use must be in the *public\_html* directory.

6. Click **Save Changes**, and then click **Close**.

7. Use a web browser to test your web site. The browser should display content from the new document root directory.

   > 👍 Tip
   >
   > If your web site does not function correctly:
   >
   > * Make sure that the new document root directory exists and contains web site files (such as *index.html* or *index.php* ).
   >
   > * Make sure the *.htaccess* file contains the correct domain name and subfolder name.

### Restoring the public\_html directory

You can restore the original configuration and use the public\_html directory as the document root again by reversing the procedure that you followed above. To do this, follow these steps:

1. Log in to cPanel.
   > 📘 Note
   >
   > If you do not know how to log in to your cPanel account, please see [this article](/docs/accessing-cpanel).

2. In the **FILES** section of the cPanel home screen, click **File Manager**:\
   ![cPanel - File Manager icon](https://static.hosting.com/kb/kb-cpanel-78-file-manager-icon.png)

3. In the left sidebar, click the **public\_html** folder.

4. In the file listing, right-click the **.htaccess** file, and then click **Rename**.

5. In the **New file name** text box, type an alternate name for the *.htaccess* file (for example, *.htaccess.bak*), and then click **Rename File**.

6. Use a web browser to test your web site. The browser should again display content from the *public\_html* directory.

   > 👍 Tip
   >
   > Alternatively, you can delete just the relevant redirection rules in the *.htaccess* file, and leave the rest of the file contents unchanged. You may want to do this, for example, if your *.htaccess* file has other directives in it that are unrelated to the document root directory.

## Related articles

* [Using .htaccess files](/docs/using-htaccess-files)

* [Default directory index page](/docs/default-directory-index-page)

* [Directory index listings](/docs/directory-index-listings)
