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?
Section titled “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
Section titled “Methods to Change the Document Root”Method 1: Using Symbolic Links (SSH Access Required)
Section titled “Method 1: Using Symbolic Links (SSH Access Required)”-
Log in to your account via SSH.
-
Move the existing
public_htmldirectory:Terminal window mv ~/public_html ~/public_html_backup⚠️ Important: Run the next command immediately to avoid downtime
-
Create a symbolic link pointing to the new directory:
Terminal window ln -s ~/new_document_root ~/public_html -
Verify the symbolic link:
Terminal window ls -l ~/public_htmlOutput should show
public_html -> new_document_root. -
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
Section titled “Method 2: Using .htaccess Redirection”If you don’t have SSH access, you can redirect requests to a subfolder via .htaccess.
- Log in to cPanel.
- Open File Manager and navigate to
public_html. - Edit or create the
.htaccessfile. - Add the following rules (replace
example.comandsubfolder):
# Redirect main domain to subfolderRewriteEngine onRewriteCond %{HTTP_HOST} ^(www\.)?example.com$RewriteCond %{REQUEST_URI} !^/subfolder/RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule ^(.*)$ /subfolder/$1
# Load index file from subfolderRewriteCond %{HTTP_HOST} ^(www\.)?example.com$RewriteRule ^(/)?$ subfolder/index.html [L]Restoring the Original Document Root
Section titled “Restoring the Original Document Root”-
Symbolic Link Method
Section titled “Symbolic Link Method”Terminal window rm ~/public_htmlmv ~/public_html_backup ~/public_html -
.htaccess Method: Rename or delete the
.htaccessrules that redirect traffic.