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.

CM
Christopher Maiorana

Christopher Maiorana joined the InMotion community team in 2015 and regularly dispenses tips and tricks in the Support Center, Community Q&A, and the InMotion Hosting Blog.

More Articles by Christopher

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

  • Does it affect google ranking ? 0r google will treat it similarly as we open the default root page(index.html).

    • Hello and thanks for contacting us. Did you make sure to replace “example.com” and “subdirectory” appropriately? Also, did you put an index file in the new directory?

  • i did this but on navigation when i click on ther page it is not showing. instead of www.mydomain.com/about it shows www.mydomain.com/subdomain/about
    Please help me to get rid of this problem.
    Thanks

    • Sorry for the troubles. I check the article and the code block was not displaying correctly. I have updated it to show the correct rewrite rules, and they should work now.

Was this article helpful? Let us know!