Change the default main domain folder to subfolder
Many web hosting providers, for example BlueHost and JustHost (only $2.95/month now – when I registered, it was $6.95 Jesse!), 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 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.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?main\-domain.com$ [NC]
RewriteCond %{REQUEST_URI} !^/sub\-folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /sub-folder/$1
RewriteCond %{HTTP_HOST} ^(www.)?main\-domain.com$ [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 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.








nice post. thanks.
Hey, I am checking this site from my Blackberry and it looks kinda funky. Thought you’d want to know.
Hey, just wanted to shout thanks! Stumbled upon some articles that were ‘explaining’ how to do this but none worked out of the box as yours. Thanks heaps!
Thanks a lot for nice feedback, Tihomir ^_^
I don’t usually comment but I can’t keep back, wonderful work right here.
Thanks.. Works perfect..!!
Many thanks, with your help my main domain finaly points to right subfolder
Hi! Thanks for the article! Could you tell me please, can I test this approach locally with my XAMPP?
saved my day. thank you!
Awesome!!
Thank you for “How it Works” section.
Alex
You actually make it seem really easy with your presentation however I in finding this matter to be actually one thing which I feel I would by no means understand. It sort of feels too complicated and extremely vast for me. I am having a look ahead to your next post, I will try to get the cling of it!
Thanks a million, just what I was looking for … making my life with WP much easier now …
Vacheslav – look into using a host file especially if you are going to configure stuff locally first like WP – made that mistake before only to discover when uploading to server that all my db configurations were pointing to local paths on my machine … lesson learnt though
Hi,thanks for the tutorial.I am new to .htaccess . Actually my requiement is in my site there is a registration form. At the time of registration one subdomain will dynamically created with the name of the registered person. After that he type the url like abc.test.com i would like to show the index page in root domain ie in test.com. But dynamically subdomains are created. But when i try to access abc.test.com it shows the directory index in abc folder. Can you please help me how to redirect from sub-domain to main domain having the same sub-domain url in address bar. Here sub-domains are created dynamically. Thanks in Advance.
Hi Ajay,
Not sure how your hosting server is functioned.
However, you may try to put
RewriteRule ^(.*) http://test.com/$1 [L]
in your subdomain .htaccess file so it will show the content of test.com while still retain the subdomain in the URL
Hi Chanon ..
Thank you very much. This is working fine. Hosting server is at hostgator.Client takes vps level 1 plan. cPanel Version 11.30.4 (build 6).But there is a problem.While creating subdomain actually what i did is i just created only folder in public_html/ dynamically. In that by default cgi-bin folder is created. Suppose if i am having abc.test.com/products.php?pid=12&catid=15 then how can i show? What should i wirte in htaccess file .Actually that sub-domain folder is empty,except this .htacess file.Please help me in this regard. Once again i am very thank ful for your quick response. Waiting for your reply. Thank you very much.
Ajay.
Hi Chanon ..Again story comes to start.It is not working. I will tell you the exact structure of cPanel.
public_html / is the root folder. When i create sub-domain dynamically (ex: sub-doamin is abc)it is created under pulic_html /abc.Here sub-domain is not pointing to the root domain.But when i create sub-domain manually i am pointing to the root domain.(Ex: subdomain name: abc .test.com Document root public_html/).In this scenario .htaccess is working fine. But while creating dynamically (Ex: subdomain name: abc .test.com Document root public_html/abc/).In this scenario again it shows the index of sub-domain, and when i placed htaccess file in subdomain with out having RewriteEngine on
it shows directory structure of subdomain. If i put the line RewriteEngine on it is redirecting to root domain and in url also it shows test.com. Please check and help me .Thank you.
Ajay
Hi Ajay,
I’m getting a bit confuse here.
In your subdomain (public_html/abc) .htaccess file should says:
RewriteEngine on
RewriteRule ^(.*) http://test.com/$1 [L]
Then, when you call
http://abc.test.com/products.php?pid=12&catid=15
It will show the content of public_html/products.php?pid=12&catid=15 while the URL still remain exactly the same.
This is definitely work. You may test this with your local server (e.g. XAMPP)
If it’s working on your local server but not on hostgator, it means there’s some specific setting on hostgator. You may need to pass this question to their support.
Or at least, pass me the content of .htacess in your main domain and subdomain. So I may have a better understanding how to help you solve the issue.
Hi Chanon ..
Please read second post. If i create subdomain manually it is working fine with the htaccess what you send. But if i create subdomain dynamic it will show the folder index of subdomain. And just i placed the two lines of htaccess code what you send. I placed that htaccess file in subdomain only. Please read my second post.
Thank you
Ajay
Hi Chanon ..
I am eagerly waiting for your response.
Thanks,
Ajay
Hi Ajay,
I think I can’t help you at this stage. The normal code doesn’t work mean there is some automatically setting on server admin level when you create sub-domain dynamically.
You may need to get assistance from your web hosting support directly.
Cheers,
Hi Chanon,
Anyway thanks for your attention.
Thanks Ajay.
Wish you solve the problem soon.
Cheers
Chanon
Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me.
Hi,
The above post is very good. But i am not sure if it will suit my situation. I have a main domain and there is a sub domain which is the first request url. If you want to incorporate a htaccess in the subdomain how can i do it ?
Please Help.
Thanks in advance.
Hi Chanon,
I need your help. Please refer the following link for the scenario and suggest me what I should do?
http://wordpress.org/support/topic/copied-website-in-sub-folder-redirects-the-url-to-root-how-to-change
Thanks,
Shanthosh Krishnakumar
I found out the solution. The following worked for me.
# BEGIN WordPress
RewriteEngine On
RewriteBase /sites/kidstoys/
RewriteRule ^index\.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /sites/kidstoys/index.php [L]
# END WordPress
Thanks,
Shanthosh Krishnakumar
Website Developer
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
@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 ……….
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!