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 http://main-domain.com/add-on1/add-on_files.files.

.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 main-domain.com 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 www.my-domain.com, you need to use it as www.my-domain.com. 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 main-domain.com or www.main-domain.com 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 main-domain.com (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.

  • Mick

    You are a legend! Thanks heaps!

  • Hakim

    Wow thanks a bunch for the article. Was stuck on this for over a day.

  • BMP Studio

    Great tutorial! it works perfect for us, thanks a lot 😀

  • BMP Studio

    Great tutorial! it worked perfect for us, thanks a lot 😀

  • I donate $100/mo, why?

  • My Photography

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