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 A2 Hosting 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.
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:
mv ~/public_html ~/public_html_backup
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
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 or by logging in to 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/
Use a web browser to test your web site. The browser should display content from the new document root directory.
If your web site does not function correctly:
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:
rm -rf ~/public_html
Then repeat steps 2 and 3.
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:
rm ~/public_html
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:
mv ~/source_directory ~/public_html
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:
In the list of files, right-click .htaccess, click Edit, and then click Edit again.
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]
Use a web browser to test your web site. The browser should display content from the new document root directory.
If your web site does not function correctly:
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:
Use a web browser to test your web site. The browser should again display content from the public_html directory.
Subscribe to receive weekly cutting edge tips, strategies, and news you need to grow your web business.
No charge. Unsubscribe anytime.
Did you find this article helpful? Then you'll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.
We use cookies to personalize the website for you and to analyze the use of our website. You consent to this by clicking on "I consent" or by continuing your use of this website. Further information about cookies can be found in our Privacy Policy.