# Changing the document root directory

> Serve your website from a different folder than public_html using symbolic links or .htaccess redirection.

Source: https://helpcenter.ruachost.com/developers/web-servers/changing-the-document-root-directory/

The **document root directory** is the folder where your website files are stored and served to visitors. By default, on **ruachost.com** shared hosting accounts, this is the `/home/username/public_html` directory. You may want to change the document root if you’re testing a new application, staging a site, or reorganizing your file structure.

## Why Change the Document Root?

- Test a new application without affecting the live site.
- Stage a website before launch.
- Organize files into separate directories for different projects.
- Improve security by isolating public files from private ones.

## Methods to Change the Document Root

### Method 1: Using Symbolic Links (SSH Access Required)

1. Log in to your account via SSH.
2. Move the existing `public_html` directory:

   ```bash
   mv ~/public_html ~/public_html_backup
   ```

   ⚠️ Important: Run the next command immediately to avoid downtime
3. Create a symbolic link pointing to the new directory:

   ```bash
   ln -s ~/new_document_root ~/public_html
   ```
4. Verify the symbolic link:

   ```bash
   ls -l ~/public_html
   ```

   Output should show `public_html -> new_document_root`.
5. Test your site in a browser.

???? Note: The server’s cron job will reset permissions every 4 hours (e.g., 4:12, 8:12, 12:12). To minimize downtime, make changes just before one of these times.

### Method 2: Using `.htaccess` Redirection

If you don’t have SSH access, you can redirect requests to a subfolder via `.htaccess`.

1. Log in to **cPanel**.
2. Open **File Manager** and navigate to `public_html`.
3. Edit or create the `.htaccess` file.
4. Add the following rules (replace `example.com` and `subfolder`):

```
# Redirect main domain to subfolder
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subfolder/$1

# Load index file from subfolder
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$
RewriteRule ^(/)?$ subfolder/index.html [L]
```

## Restoring the Original Document Root

- ### Symbolic Link Method

  ```bash
  rm ~/public_html
  mv ~/public_html_backup ~/public_html
  ```
- **.htaccess Method:** Rename or delete the `.htaccess` rules that redirect traffic.

> **Important Notes:**
- For addon domains, change the document root via **cPanel → Domains → Document Root** instead of these methods.
- Always back up your site before making changes.
- Incorrect configuration may cause downtime or errors.
