Skip to main content

. htaccess Tips & Tricks

Force HTTP to HTTPS (SSL):

RewriteOptions inherit
Options -Indexes
#SecFilterEngine Off

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

<IfModule mod_alias.c>
</IfModule>

Change default DocumentRoot directory

This is possible via .htaccess. You will need to add the following code in your .htaccess file:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain.com$ 
RewriteCond %{REQUEST_URI} !folder/
RewriteRule (.*) /folder/$1 [L]

Replace ‘domain.com‘ with your domain name and ‘folder‘ with your custom folder name where you kept the index.

Introduction

While there are tools in cPanel that will allow you to change the document root for an addon domain, making this change for the main cPanel account is a bit more involved and requires work on the command line as the root user.

Procedure

  1. Access the server over SSH as the root user.
  2. Edit the line beginning with documentroot in the following two files related to the particular cPanel user you're working with. Your new directory still needs to exist within the user's account under /home/username. Replace username and domain.com with the specific domain in the following examples. It's a good idea to make backups of these files before making edits in case something does go wrong:
    1. /var/cpanel/userdata/username/domain.com
    2. /var/cpanel/userdata/username/domain.com_SSL
  3. After you've saved your changes, run the following commands. These commands update the cache files on the system that cPanel uses, the cPanel configuration, as well as back up and restart the Apache service:
    /scripts/updateuserdatacache
    /scripts/updateuserdomains
    cp /etc/apache2/conf/httpd.conf{,.bak-`date +%Y%m%d`}
    /scripts/rebuildhttpdconf
    /scripts/restartsrv_httpd

After performing this work the domain now loads content from the new document root you have specified. Making the changes in this manner ensures your customizations will continue to work after updates have been applied to the system.

How to change the document root for a cPanel account