Site5 - Built For Designers & Developers MENU

How To Redirect Your Domain To A Subfolder Using .htaccess

10 Comments

Sometimes the simple things are the hardest to figure out and today we are going to show you a simple way to redirect your primary domain to a subfolder on that domain. For the purposes of this tutorial it will redirect to a subfolder at /blog. This is useful if your WordPress blog is installed to a subfolder and you want the main domain to forward to it.

In your public_html folder (also called the “root html” folder), you need to create a file called .htaccess. From this file we are going to use a few lines to redirect those requests in a search engine friendly way.

Add the following lines to that file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?YourDomain.com$
RewriteRule ^(/)?$ blog [L]

The only change you need to make is to change “YourDomain.com” to your domain. This will then redirect everything from the domain to the folder /blog at that domain. If you want to change that folder just edit the “blog” part.

And you are good to go! For further information on what you can do with a .htaccess file I recommend the following sites…

  • Comprehensive Guide To .htaccess – Scroll to the bottom of the page for 12 tips on things you can do with the .htaccess file. Everything from password protection to redirects to preventing directory listings.
  • .htaccess Cheat Sheet – A nice listing of different tips and tricks to use. Sparse on explanations though (that is why it is a cheat sheet).
  • .htaccess Tools – A great site that has some basic tools online that can generate everything for you. Nice article on how to detect and redirect iPhone users as well.
  • Perishable Press Stuipd .htaccess Tricks – When you finish reading this you will be a .htaccess ninja! 56 pages of easy to read documentation. At least easier to read than the official Apache docs.

Just a word of warning, Apache redirects are often a thing of voodoo magic and for really complex rules you might have to play with them for a while to get them to work how you want.

10 Comments

  • It has been quite a while that I wanted to learn this. Thank for the info. Newbies like myself need this kind of publication. Thanks

  • No problem, also we will soon have a submission area so you can submit what you would like to do. We are planning out a new resource center for customers and visitors alike!

    thanks, Ben

  • I have a .htaaccess file already in the root of our site with the following code in it

    ## START PHP
    AddType application/x-httpd-php .php
    Action application/x-httpd-php /cgi-bin/php4.cgi
    ## STOP PHP

    Do I just add the redirect code to the existing file? and would the redirect code be the same if I had two sites housed in the same area.

    http://www.site1.co.uk in the root
    http://www.site2.org in a subfolder in the root

  • Yep you can add it to that same file, and just change the rewrite rule so it has the domain you want, just check the instructions above and let me know if those make sense as those tell you where to put in your domain.

  • Tried the code above but when I enter the subsites web address the main site is still loading. Code in the htaccess file is as follows…

    ## START PHP
    AddType application/x-httpd-php .php
    Action application/x-httpd-php /cgi-bin/php4.cgi
    ## STOP PHP
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^(www.)?site.org$
    RewriteRule ^(/)?$ Sub_site1 [L]

    where site is the name of the website and Sub_site1 is the folder the default page is located.

    Any advice welcome.

  • Just before I cause you asome work, let me check something out. I may not get the answer until tomorrow night but I think that when I am typing the web address for the sub site the sub site is not arranged to direct traffic to our hosted original site this is being handled by a redirect so the code would never be called. I need to get the url redirected to the main site. Hope that works. Will let you know.

  • This may sound silly but do I leave in the question marks?
    I edited the htaccess file, took a while to get access to the remote file, ended up deleting it and then uploading a new file something to do with permissions.
    my htaccess file ended up like this…

    ## START PHP
    AddType application/x-httpd-php .php
    Action application/x-httpd-php /cgi-bin/php4.cgi
    ## STOP PHP
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^(www.)?site.org$
    RewriteRule ^(/)?$ Sub_site1 [L]

    Where “site” is the url for the default page of the new nested site and Sub_site1 is the folder where it is contained.

    I tested it but it just went to the main site, I believe that this is happening because the new url is not currently point to the IP address for our main site but is in fact being handled by a redirect. Will get them to point the domain at the main site and report back.

    Thanks for your assistance Ben.

  • No worries, yep the ? marks stay, although I think you put your site as being site.org, so that you would need to change to your domain but I think you know that.

    No problem!
    Thanks, Ben

  • This is what I was looking for. my hosting company had these features from cpanel, but these days bluehost seems to screwing up things. Thanks for this pice of information.

  • You missed one of the best resources for .htaccess (and a bit more) tips-scripts.com It’s even hosted here too :)