Redirect Primary Domain URL to SubDirectory

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.

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 “www.oscarliang.net” or “oscarliang.net” 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.

# blog.oscarliang.net
# Redirect Primary Domain URL to Sub-Directory using .htaccess

# Do not change this line.
RewriteEngine on

# Change domain.com to be your primary main domain.
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$

# 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 domain.com 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.)?domain.com$
RewriteRule ^(/)?$ subfolder/index.php [L]

11 thoughts on “Redirect Primary Domain URL to SubDirectory

  1. chris

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

    Reply
  2. Alec Caton

    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

    Reply
  3. Steve Clark

    I’ve tried this and it only seems to works for the main-domain.com

    ie. ‘main-domain.com’ masking ‘main-domain.com/website-location/’

    When you then navigate to a subpage it reverts back to the original main-domain.com/website-location/page.php instead of the main-domain.com/page.php

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

    Reply
  4. Colin

    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.

    Reply
    1. steve

      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,

      Reply
      1. Oscar Post author

        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

      2. John10

        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?

  5. Sam Rodrigues

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

    Thank you!

    Sam

    Reply

Leave a Reply

Your e-mail address will not be published. Required fields are marked *

Are you Robot? *

For prompt technical support, please use our forum IntoFPV.com. I check blog comments weekly.