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.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?main\$ [NC]
RewriteCond %{REQUEST_URI} !^/sub\-folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /sub-folder/$1
RewriteCond %{HTTP_HOST} ^(www.)?main\$ [NC]
RewriteRule ^(/)?$ sub-folder/index.php [L]

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.

  • hAPy

    Hey just wanna say Thanks ! Copy/Paste that example, replaced my values and it WORKED ! Was pleasantly surprised !
    Good job and keep the good work :)

  • Mark Murphy

    Can i just pay someone to make me some of these. I dont even understand the terminology, and the tutorial is like chinese to me

    • Chanon Srithongsook

      Hey Mark, try
      lots of cost effective developers out there…

  • afsane

    perfect 😉

  • Chris

    Thank you!

  • Azaleo A. Budok

    This code works for me…very helpful post! Thanks Chanon

  • kaybierocket

    This gave me a 505 error. please advise.

  • Razvan I.

    Hi Chanon,

    My main domain is a wordpress site and would like to move it to a subfolder of /public_html . will your solution work as is or do i have to….alter other files related to WP?

    thank you

  • Alex

    THANK YOU! :)

  • misa

    I use this one:

    RewriteEngine on

    RewriteCond %{HTTP_HOST} ^(www.)?$

    RewriteCond %{REQUEST_URI} !^/sub-folder-name-here/

    RewriteCond %{REQUEST_FILENAME} !-f

    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteRule ^(.*)$ /sub-folder-name-here/$1

    RewriteCond %{HTTP_HOST} ^(www.)?$

    RewriteRule ^(/)?$ sub-folder-name-here/index.php [L]

    make sure the last line has : index.php or maybe index.html depending on your script files.

    • Jornes

      I tried this code. But, unfortunately it says my web page has a redirect loop. What does it mean?

  • Charlie

    Will an .htaccess in the destination folder work, if you have an application that relies on url_rewrite?

  • I donate $100/mo, why?

  • My Photography

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