Redirect Primary Domain URL to SubDirectory

by Oscar

Use Primary Domain on Subdirectory and Keep subdomain working

Primary Domain on most hosting services usually use “public_html” folder (or root folder) to store its files. When you create a subdomain, it will create a sub folder in “public_html”. I found it really messy when I have too many subdomains, which is confusing with the primary domain site files and folders.

Some of the links on this page are affiliate links. I receive a commission (at no extra cost to you) if you make a purchase after clicking on one of these affiliate links. This helps support the free content for the community on this website. Please read our Affiliate Link Policy for more information.

Therefore I was trying to find a way to redirect my primary domain URL to a subfolder, while other subdomain folders still work.

This job would require a “.htaccess” file to redirect requests to the correct folder. For example anything for “” or “” would be forwarded to a specific folder, requests with other subdomains will be ignored. (so they can be forwarded to the corresponding sub-directory by the hosting server.

It works like a charm, here is the .htaccess file.

.htaccess Code for redirecting main domain request to subfolder on public_html

Create a .htaccess file on your root directory (public_html), and copy and paste the following code in the file
make the changes to the file according to the instructions.

# # Redirect Primary Domain URL to Sub-Directory using .htaccess # Do not change this line. RewriteEngine on # Change to be your primary main domain. RewriteCond %{HTTP_HOST} ^(www.)?$ # Change 'subfolder' to be the folder you want to redirect request to. RewriteCond %{REQUEST_URI} !^/subfolder/ # Don't change this line. RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # Change 'subfolder' to be the folder you want to use for your primary domain. RewriteRule ^(.*)$ /subfolder/$1 # Change to be your primary domain again. # Change 'subfolder' to be the folder you will use for your primary domain # followed by / then the main file for your site, index.php, index.html, etc. RewriteCond %{HTTP_HOST} ^(www.)?$ RewriteRule ^(/)?$ subfolder/index.php [L]

Leave a Comment

By using this form, you agree with the storage and handling of your data by this website. Note that all comments are held for moderation before appearing.


Marga 1st June 2018 - 4:25 pm

Thank you very much. It’s working on http. Not on https.


Sander 9th January 2017 - 1:24 am

Works perfect! Thank you so much for sharing

chris 2nd May 2016 - 1:38 am

Great job. Worked perfectly on today…. just moved from godaddy after 10 years… what took so long?! :)

Alec Caton 23rd March 2015 - 6:55 pm

Before I start….I know nothing. Absolutely nothing…

But, I’ve found this code in an .htaccess file in a subdirectory for my website, which is running through Wordpress. I remember the developer saying they had issues and this was a great solution (which I can’t argue, works a treat).

However, one thing I have been looking into is changing the permalinks from the default settings so the post names are clear in the URL.

Again, I’m very very limited in my knowledge, but Wordpress has a code for me to change the common permalink setting…

# BEGIN WordPress

RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress

But this doesn’t seem to work for me. I was wondering if this was because we’re being redirected to a subfolder and they are conflicting…?

This may make no sense…it doesn’t make a lot of sense to me but it’s blagging my head!

Cheers, Alec

Steve Clark 4th November 2014 - 12:43 pm

I’ve tried this and it only seems to works for the

ie. ‘’ masking ‘’

When you then navigate to a subpage it reverts back to the original instead of the

Is there a way to fix this and keep the viable root as and /website-location/ hidden?

Colin 23rd September 2014 - 11:49 pm

Thank you, thank you and thank you again!
I spent the last 48hrs looking for a redirect that would work and trying many different ways to do this.
Yours worked perfectly the first time. I am very grateful.

James Wheeler 27th April 2014 - 6:13 pm

I can’t get this to work on a godaddy “shared server” site

steve 3rd June 2014 - 12:24 am

James, I have found that godaddy sucks ass and you will never ever get a redirection on their crap to work. Their techs don’t ev en know what we are talking about. Move your stuff to A REAL HOST SERVICE,

Oscar 3rd June 2014 - 11:34 am

Moved away from gomummy 1 week after signing up! what a crap service it was!
ended up with bluehost, and have been using it for 1 year now, and I am a happy man! :-D

John10 26th June 2014 - 9:01 am

Hi Steve, I just realized too that this code which is all over the net, doesn’t work on the trashy Godaddy servers. So, I’d like to ask you, which is a real host service for you? which one you recommend?

Sam Rodrigues 15th January 2014 - 10:45 am

That is a perfect .htaccess redirection rule: totally transparent and effective.
Used it and tested.

Thank you!