Pointing the domain to a folder within the root directory

Last updated on December 15, 2009. Tags: ,

This .htaccess trick is useful in cases wherein your site (1) undergoes a major overhaul or (2) when you want to have two different websites in just one hosting.  The first case is when you want to keep the original site online while developing the second version and then switch to the second version without any interruption. In the second case, the domain and hosting provider will usually do it for you, but if not you can do it on your own using this trick.

Supposed you have a website domain.com and it is currently online with lots of visitors. You want to overhaul it but you do not want your visitors to experience interruption so you decided to create a folder domain.com/new/ and created the entire new version of the website in that folder. Moving the content of the /new/ folder to the root directory will take some time and interrupt your site visitors. What you can do is to point the domain to the /new/ folder itself.

Note that I use the term "point" instead of redirect. Pointing a domain is different from redirecting. If you redirect domain.com to domain.com/new/, you can type domain.com in the address bar and press enter but when the page loads itself to the browser, you will see the URL in the address bar changing to domain.com/new/. If you are pointing the domain, doing the same thing will still show the content of domain.com/new/ but the URL in the address bar will not change.

To point the domain to a folder, simply open the .htaccess in your root directory and insert the code below. If your root directory has no .htaccess file, you can just create it. Note that some hostings have default setting where .htaccess is a hidden file and you need to configure something first in your setting to see the .htaccess.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteCond %{REQUEST_URI} !^/new/
RewriteRule (.*) /new/$1

Simply replace the domain domain.com and the folder /new/ with whatever applies to your website. Accessing domain.com will now show you domain.com/new/ but the URL in the address bar of your browser will still be domain.com.

You can also use this if you have two domains pointing to the same hosting and you want use them for two different websites. Say you have domain.com and domain2.com; you can put the content for domain.com in the root directory and the content of domain2.com in domain.com/site2/ and point domain2 in that folder (using the same code, just replace domain.com$ with domain2.com$, and /new/ with/site2/.

You can also use this if the site is powered by WordPress. The code does not come in conflict with the one that enables permalinks  should in case they are in the same .htaccess file (you can just insert this code immediately after the WordPress code).

Rooms for further study

Parameter handling

Posted by Greten on November 9, 2009 under Server Configuration

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • StumbleUpon
  • Technorati

Related Posts

You might also be interested (randomly generated):

Read Comments

  1. Posted by armandoy on 07.16.10 8:33 am

    hi,, thanks for the great tutorials,,
    i have a question,, e.g
    how do we point http://domain.com/cms/
    into http://domain.com/

    whatever the content in /cms/ it will be displayed in http://domain.com site

    thanks,,
    -man

    Edited: italicized slashes to prevent automatic hyperlink insertion – Greten

  2. Posted by Greten on 07.16.10 6:18 pm

    Hi Armandoy

    You might want to try the technique discussed in one of the related articles article: http://codeleet.hub.ph/using-htaccess-301-redirect-in-subfolders/

    However, this redirects the folder to another domain, not to the same domain that contains the folder. If it works in your case, please let us know. Otherwise, still let us know so I can experiment in such case when I have time. Thank you!

Post Comments

Please double check your comment before clicking the "Post" button. Once you clicked it, there will be no way for you to edit your comment.





* Required. Your email will never be displayed in public.