Web Hosting Knowledge Base Web Hosting Knowledge Base
  • Home
Get Started
Web Hosting Knowledge Base Web Hosting Knowledge Base
Get Started
Web Hosting Knowledge Base Web Hosting Knowledge Base
  • Home
loading
  1. Home
  2. Developer Corner
  3. Apache web server configuration
  4. How to change the document root directory
Updated on June 14, 2024

Developer Corner

  • Folder icon closed Folder open iconDeveloper Corner Resources
    • MySQL development
      • How to connect to MySQL using Node.js
      • How to connect to MySQL using Microsoft .NET
      • How to manage MySQL databases, users, and tables from the command line
      • How to back up MySQL databases using AutoMySQLBackup
      • How to reset the MySQL root password
      • MySQL server runs out of memory or does not start
      • MySQL user privileges on shared servers
      • How to create and execute MySQL stored functions and procedures
      • Using MySQL views
      • Using MySQL triggers
      • Working with MySQL database engines
      • How to check MySQL database and table sizes
      • How to repair MySQL databases and tables
      • How to enable the slow query log in MySQL
      • How to rename a MySQL database
      • How to use indexes to improve MySQL query performance
      • How to restrict MySQL port access
      • How to install phpMyAdmin on managed hosting accounts
      • How to create a MySQL installation for local testing
      • How to troubleshoot MySQL definer issues
      • Using SELECT INTO OUTFILE in MySQL
      • How to determine the MySQL version
      • How to Fix the MySQL Error 1064
    • Ruby developer information
      • How to install Ruby Gems
      • How to use the Interactive Ruby Shell to troubleshoot gems
      • How to set up multiple versions of Ruby with the cPanel Ruby Selector
      • How to configure a Ruby on Rails application on a shared hosting account
    • Linux for the developer
      • How to manage the inode count
      • How to determine high disk usage locations
      • How to use screen
      • How to monitor resource usage
      • How to use rsync
      • How to use cURL
      • How to disable e-mail notifications from cron jobs
      • How to use ImageMagick
      • How to change the time zone in the Linux shell
      • Cron jobs reset to 15-minute intervals
      • How to install and configure Advanced Policy Firewall
      • How to configure a firewall using iptables
      • How to set the default text editor in Linux
      • How to determine a server's memory usage
      • How to generate and verify file checksums on Linux
      • How to install the EPEL repository on CentOS
      • How to run a cron job automatically after a server reboot
      • Conntrack sessions
      • How to install and configure UFW (Uncomplicated Firewall)
    • Version control systems
      • How to use Git
      • How to configure a Git client
      • How to configure Subversion (SVN)
      • How to set up Subversion (SVN) access for multiple users
      • How to use the Mercurial version control system
      • How to configure Concurrent Versions System (CVS)
      • How to configure and use a newer version of Git
      • '403 Forbidden' error message when you try to push to a GitHub repository
    • Client-side technologies
      • How to use the jQuery JavaScript library on your hosting account
      • How to install Ember.js on managed hosting accounts
      • How to use the markItUp JavaScript plugin
      • How to Use Flash on Your Website
      • Does Hostvento Hosting Support MP3 Hosting Files?
      • Hostvento Hosting MIDI (Musical Instrument Digital Interface) Support
      • How to use React on your hosting account
    • Application deployment information
      • How to install the Docker application deployment environment
      • How to install a LAMP stack on an Ubuntu server
    • How to add CAPTCHA protection to your web site
    • How to control search engines and web crawlers using the robots.txt file
    • How to set up URL frame forwarding
    • SQLite information
      • How to connect to SQLite from the command line
      • How to connect to SQLite using PHP
      • How to connect to SQLite using Python
    • Using Lua on Hostvento Hosting servers
    • How to determine if your account uses CageFS
    • How to set up a virtual machine with VirtualBox for local testing
    • Web testing and development
      • How to create a PostgreSQL installation for local testing
      • How to create an Apache installation for local testing
      • How to install development tools on an unmanaged server
    • How to create persistent Node.js applications on shared and reseller hosting accounts
    • How to enable Cross-Origin Resource Sharing (CORS)
    • How to configure frames with X-Frame-Options header
    • Developing ADA-compliant web sites
    • Nginx web server configuration
      • How to install the Nginx web server
    • How to install and configure the AWS Command Line Interface (CLI)
    • How to edit hosts file on Mac
    • How to disable automatic creation of the robots.txt file
    • How to use an SSL certificate in a Node.js app
  • Folder icon closed Folder open iconHow to enable and disable PHP error messages
  • Folder icon closed Folder open iconHow to enable PHP error logging
  • Folder icon closed Folder open iconHow to enable and disable the PHP register_globals directive
  • Folder icon closed Folder open iconHow to use the PHP magic quotes directive
  • Folder icon closed Folder open iconHow to change the PHP time zone setting
  • Folder icon closed Folder open iconHow to enable and disable the PHP allow_url_fopen directive
  • Folder icon closed Folder open iconPHP max_execution_time Guide
  • Folder icon closed Folder open iconHow to change the maximum upload file size for PHP scripts
  • Folder icon closed Folder open iconHow to connect to MySQL from the command line
  • Folder icon closed Folder open iconHow to connect to MySQL using PHP
  • Folder icon closed Folder open iconHow to connect to MySQL using Perl
  • Folder icon closed Folder open iconHow to import and export a MySQL database
  • Folder icon closed Folder open iconHow to set up a remote MySQL connection
  • Folder icon closed Folder open iconUsing MySQL client applications
  • Folder icon closed Folder open iconHow to convert a MySQL database to UTF-8 encoding
  • Folder icon closed Folder open iconHow to convert the time zone in MySQL
  • Folder icon closed Folder open iconHow to back up MySQL databases using cron jobs
  • Folder icon closed Folder open iconInformation schema database in MySQL
  • Folder icon closed Folder open iconHow to connect to PostgreSQL using psql
  • Folder icon closed Folder open iconHow to connect to PostgreSQL using PHP
  • Folder icon closed Folder open iconHow to connect to PostgreSQL using Perl
  • Folder icon closed Folder open iconHow to import and export a PostgreSQL database
  • Folder icon closed Folder open iconHow to set up a remote PostgreSQL connection
  • Folder icon closed Folder open iconHow to use .htaccess files
  • Folder icon closed Folder open iconHow to change the default directory index page using an .htaccess file
  • Folder icon closed Folder open iconHow to enable and disable directory index listings using an .htaccess file
  • Folder icon closed Folder open iconHow to enable data compression using the mod_deflate module
  • Folder icon closed Folder open icon'403 Forbidden' error message
  • Folder icon closed Folder open iconHow to enable pathname information in URLs using the AcceptPathInfo directive
  • Folder icon closed Folder open iconHow to troubleshoot CGI scripts
  • Folder icon closed Folder open iconWhat is PHP?
  • Folder icon closed Folder open iconPHP script basics
  • Folder icon closed Folder open iconHow to view PHP settings with the phpinfo() function
  • Folder icon closed Folder open iconControlling PHP settings with a custom php.ini file
  • Folder icon closed Folder open iconUsing php.ini directives
  • Folder icon closed Folder open iconHow to run PHP scripts from cron jobs
  • Folder icon closed Folder open iconSetting the PHP include path
  • Folder icon closed Folder open iconHow to determine if a PHP function is available
  • Folder icon closed Folder open iconHow to fix '500 Internal Server Error' while running PHP
  • Folder icon closed Folder open iconPython script basics
  • Folder icon closed Folder open iconPerl script basics
  • Folder icon closed Folder open iconHow to troubleshoot Perl scripts
  • Folder icon closed Folder open iconHow to use the shebang
  • Folder icon closed Folder open iconPostgreSQL development
    • How to connect to PostgreSQL using Python
    • How to use PostgreSQL client applications
    • How to back up PostgreSQL databases using cron jobs
    • How to manage PostgreSQL databases and users from the command line
    • How to determine the PostgreSQL and PostGIS versions
    • How to determine the size of PostgreSQL databases and tables
    • How to rename a PostgreSQL database
    • ECCN and HTS numbers for PostgreSQL
  • Folder icon closed Folder open iconApache web server configuration
    • How to change the document root directory
    • How to add or remove the www prefix in domain URLs
    • How To Use Apache Access Log & Apache Error Log
    • How to rewrite URLs in Apache with the mod_rewrite module
    • How to use Server-Side Includes (SSI)
    • How to use the mod_speling Apache module
    • How to modify HTTP headers using .htaccess files
    • How to configure caching with the mod_expires module
    • How to disable entity tags (ETags)
    • How to use keep-alive connections to improve performance
    • How to control site access based on a visitor's country
    • How to turn off caching using .htaccess
    • How to install Apache Tomcat
    • How to protect a website folders using .htaccess
  • Folder icon closed Folder open iconPHP development resources
    • How to change PHP settings and versions on managed hosting accounts
    • How to set up custom .htaccess files for PHP settings
    • Using PHP directives in custom .htaccess files
    • Using PHP to send e-mail messages
    • How to install and configure PEAR packages
    • How to troubleshoot 'String could not be parsed as XML' error message in PHP
    • How to use the Exif extension in PHP
    • How to resolve some common issues after a PHP upgrade
    • How to use a custom php.ini file for cPanel cron jobs
    • How To Use PHP Sessions
    • ionCube PHP Loader support
    • How to specify the MySQL character set in PHP
    • How to fix the 'Unable to allocate memory for pool' PHP error
    • How to optimize Symfony using APC
    • How to use the internationalization (intl) extension in PHP
    • How to install PHP composer
    • How to determine if APC is installed on a server
    • How to use the MailChimp API with PHP
    • 'Allowed memory size exhausted' error message in PHP
    • How to use ionCube Loader with different PHP versions
    • How to enable PHP opcode caching
    • How to send e-mail using PHPMailer
    • How to harden PHP 7 and earlier versions using cPanel
    • Custom PHP settings per directory with .user.ini files
    • ionCube PHP Loader unavailable on PHP 8.0
    • How to change the PHP version for a specific directory
  • Folder icon closed Folder open iconPython
    • How to use virtualenv and pip
    • How to activate a Python virtual environment from a script file
    • How to send e-mail messages using Python
    • How to configure and use a newer version of Python
    • How to install and configure Django on unmanaged servers
    • How to install and configure Django on a Linux shared hosting account
    • How to install and configure Flask on a Linux shared hosting account
    • How to install FastAPI on unmanaged servers
  • Folder icon closed Folder open iconPerl Information Overview

How to change the document root directory

Estimated reading: 8 minutes 128 views

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 Hostvento 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.

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.

Method #1: Using 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.
  2. At the command prompt, type the following command:Copymv ~/public_html ~/public_html_backup
    • 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:
    Copyln -s ~/new_document_root ~/public_htmlYour 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/
  4. 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:
    • 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:Copyls -l ~/public_htmlYou should see output similar to the following, where username represents your account username:/home/username/public_html -> /home/username/new_document_rootIf you do not see similar output, then the symbolic link is not set up correctly. Type the following command:Copyrm -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.
  2. At the command prompt, type the following:Copyrm ~/public_html
    • 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:
    Copymv ~/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: Using 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.If you do not know how to log in to your cPanel account, please see this article.
  2. In the FILES section of the cPanel home screen, click File Manager:cPanel - File Manager icon
  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.
    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] 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.
    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.If you do not know how to log in to your cPanel account, please see this article.
  2. In the FILES section of the cPanel home screen, click File Manager:cPanel - File Manager icon
  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. Test your website with a web browser. Once more, the public_html directory’s content ought to appear in the browser.
  7. As an alternative, you can remove only the pertinent redirection rules from the.htaccess file while leaving the remaining contents unaltered. If your.htaccess file contains other directives relating to the page root directory, you might wish to do this.You also need to alter the security keys in the wp-config.php file that are used to encrypt cookies if you believe that your site has been compromised. Changing your passwords alone will not cut it, as an attacker can still be able to access your site using a legitimate cookie.

Still stuck? How can we help?

How can we help?

Was this page helpful? Yes No

Share this Doc

How to change the document root directory

Or copy link

Clipboard Icon
CONTENTS
  • Method #1: Using symbolic links
    • Restoring the public_html directory
  • Method #2: Using redirection with .htaccess
    • Restoring the public_html directory
Leaf Illustration

© 2023 All Rights Reserved by Hostvento