Change the default main domain folder to sub-folder

Many web hosting providers, for example  BlueHost and  JustHost,  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.

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.)?$ [NC]
RewriteCond %{REQUEST_URI} !^/sub_folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /sub_folder/$1
RewriteCond %{HTTP_HOST} ^(www.)?$ [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.


  1. found your site on today and really liked it.. i bookmarked it and will be back to check it out some more later

  2. This blog is cool, keep up the good work!