![](https://www.inmotionhosting.com/support/wp-content/uploads/2021/07/How-to-Redirect-Your-Primary-Domain-to-a-Subdirectory-1024x538.png)
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.
How to Redirect to Subfolder
Follow along with the steps below to learn how to redirect to a folder:
- Log into cPanel
- Choose File Manager under Files
- Edit the
.htaccess
file
Now you that you are editing the .htaccess
file, add the following code to the 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]
Make sure to update the bold areas (“example.com” and “subdirectory”) with your own values unique to your site.
Make sure to Save Changes to the file when you’re done.
Well done! You have completed this tutorial. Now, users landing on your domain will be redirected to the subdirectory you selected.
Great article. It was inspiring, however, I did not succeed I redirecting my website.
Until now when using drupal7/9 I copied all subfolders and files in the root.
Now I have created drupal9 site using composer and the stricter changed. On localhost I can open site from http://localhost/sitename/web/.
Webhosting provider does not allow webhosting redirect directly to subfolderis there way how to change opening website not from root bud from subfolder, so that site was iopning from www.sitename.com not sitename.com/web.
Thank you
Libor
Hi Libor, it sounds like you’re trying to set up a parked domain or alias. While the linked article should be applicable for most cPanel based accounts, the specifics can vary from host to host. While we cannot provide troubleshooting for the specifics of other hosting, we hope that helps narrow things down for you!
Thank you so much, it’s work fine for me 🙂
So, this is redirecting, but showing the subdomain in the address bar. How do I point the primary domain to a subdirectory as the main website? I want the subdomain WordPress as the primary domain site.
Hello RY Design,
Thank you for your question. It sounds like you are trying to change the document root for your site. If you are on a shared server, you will need to ask our Support team to change the document root for your primary domain from public_html to the desired folder. If you are on a VPS or Dedicated server, you can change this via WHM.
Best Regards,
Alyssa K.
Hi,
I get the redirection but the site URL on the browser shows as “domain.com/subdirectory”
Hello, and sorry to hear that you’re running into issues with the redirect.
If that is happening, it is most likely that the code in .htaccess is still using the placeholder information in the example.
When you add the code to your .htaccess file, please be sure to change the placeholder information to your actual site’s domain name and subdirectory. Hope that helps!
Does it affect google ranking ? 0r google will treat it similarly as we open the default root page(index.html).
As long as the redirect is done correctly it should not affect the google ranking.
You saved my day! Thanks a lot!
I followed this steps but always ends in /cgi-sys/defaultwebpage.cgi when requesting my domain
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.
Is this functionally different from using the redirect feature in cPanel under Domains?
No, the only difference is when you use the cPanel redirect tool it automatically adds the above rules to the .htaccess file for you.
Thank you,
John-Paul