Change the default main domain folder to subfolder

Many shared hosting providers, for example  BlueHost.  allow customers to host more than one domain from a single purchase (add-ons). Add-on domains usually point itself to a sub-folder under public_html or www. However, the main domain recognizes public_html as its root, which may lead to some development issues.  This article explains why it is good to have the main domain sitting under a sub folder and how to do code to make it in action.

Note that, I’m now moving to Digital Ocean which offer a cheap VPS hosting (as cheap as shared host). If you have basic idea about how to setup LAMP stack on your machine, I suggest you go ahead with them and you will no longer worry about sub-directory issues. Click through this link and you will get $10 for trial. After spend all trial credit up (which is about 2.5 months), you can discontinue if you don’t like them.

The Benefits

Having main domain sitting in sub directory, it will be easy for website maintenance purpose. For example, you can make a backup/restore of files in the main domain with out interrupt the add-ons domain’s files. For example, if you have main domain under root of public_html, once you make zip to compress public_html folder, everything including add-ons’ files will be zipped and they all will be reset when you restore (you can’t restore it separately ).

In addition, move main domain to sub directory prevents global access to add-ons files. For instance, if public_html recognized as a start of main domain folder, add-ons files may accessible at

.htaccess is a solution

Fortunately, Apache server provide a very handy mod-rewrite module, .htaccess. This file can overwrite the server setting and redirect all requests from main domain to the target sub folder. Simply put the code below in .htacces in public_html folder. Replace with the actual main domain’s name. Don’t forget to use escape string () in RewriteCond as it use regular expression to determine the match. For example if your domain spell like, you need to use it as For more information google for .htaccess regular expression.

How it works?

line 1 is to ensure that server already know that you going to use mod-rewrite

line 2 – if the request from http is or go to next line (else abort)

line 3 – if the request destination is not the folder /sub-folder go to next line

line 4 – if the requested name is not an existed file in public_html directory

line 5 – if the requested name is not an existed directory in public_html directory

line 6 – forward request to /sub-folder/

line 7 – if the request domain is (with out any string afterward), go to next line

line 8 – forward request to the default file under sub-folder directory (in this case, index.php)

That’s it. If you never experience with .htaccess, it will be a little bit confused. Therefore, just copy .htaccess code and replace your main-domain and sub-folder.

Please feel free to use comment below if you need help.

  • Mark

    How can I adapt this code for https? It’s not working with https from my tests and I don’t know how to fix it…thanks

  • Chanon

    @Shanthosh: Thanks for sharing :)

    @Mark: Try using https port (443) in RewriteCond
    I haven’t test this yet but it should work.

    RewriteCond %{SERVER_PORT} 443
    RewriteRule ……….

  • Kacey

    This is probably the best .htaccess post I’ve ever discovered. I have three websites pointing to the same account, and I worked on a solution like this for nearly two days. A few Google searches later I found this post. Thanks a bunch for your contribution!

    • Chanon Srithongsook

      Thanks Kacey, That’s encouraging… :)

      • Patrick Sims

        Any help would be greatly appreciated. I want to continue using the htaccess file to point my site access to my ssl. I can do that but another one of my domains then redirects to my primary site. It works fine if I don’t use the htaccess, but my primary domain is then no longer on my ssl. Again, any help is appreciated. I just haven’t figured out htaccess syntax yet.

  • Rolf Rolf

    Thanks, just what I was looking for, did the trick for my little family photo forum which was sat in a subdirectory.

  • Ricardo Maldonado Mora


  • Raz Peel

    Your amazing for explaining those lines individually. Thanks a bunch!

  • JohnyApacheUser

    Thanks a lot for this amazingly helpful post!

  • Koko Boko

    This really sucks! It doesn’t explain what exactly to replace in the code!

  • Alan Hugo

    Gracias por tu ayuda!!

  • Neil

    Doesn’t this create a “temporary” redirect? I’d like to make it a 301 permanent redirect. Can you identify what I would have to change to do that?

    • Chanon Srithongsook

      Hey Neil,

      The code in this article internally links URL to the actual files in WordPress system. The URL remained the same and there’s no redirection at all (at visitors’ sight).

      To do the 301 redirection, simply put in the line below:

      RewriteRule ^URL_TO_MATCH$ [R=301,L]

  • I donate $100/mo, why?

  • My Photography

  • Above the crowd Cage Side forward Walk away Endless rain Vivid Sydney Before Dawn