How to Redirect Your Primary Domain to a Subdirectory

By default, your primary domain document root is set as the public_html directory. This is where you can place live website files for serving to the web. Any files in this directory will be rendered and served as part of a website.

However, in some cases, a situation may arise in which a document root change is needed. For shared servers, it is necessary to contact Live Support. However, VPS and Dedicated hosting customers with root access can use the steps mentioned in our full guide on Changing the Document Root for Your Domain. This is considered advanced server administration because it involves editing your Apache configuration.

In this article, we will show you an easier way to accomplish a document root change without requiring root access or editing your system’s configuration files. We will show you how to redirect your primary domain to a subdirectory. With the redirect code we provide below, users landing on your primary domain will be redirected to the contents of a subdirectory of your choice.

  1. Log into cPanel

     
  2. Under Files choose File Manager

     
  3. Edit the .htaccess file

     
  4. Add the following code to the .htaccess file:

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^(www.)?example.com$
    RewriteCond %{REQUEST_URI} !^/subdirectory/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /subdirectory/$1
    RewriteCond %{HTTP_HOST} ^(www.)?example.com$
    RewriteRule ^(/)?$ subdirectory/index.html [L]

    Making sure to substitute example.com for your primary domain and subdirectory for the subdirectory to which you are redirecting the domain.

     
  5. Save the file by clicking Save Changes

     

Well done! You have completed this tutorial. Now, users landing on your domain will be redirected to the subdirectory you selected.

Thoughts on “How to Redirect Your Primary Domain to a Subdirectory

Leave a Reply