Pointing the domain to a folder within the root directory

Last updated on December 4, 2011. 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
  • Supposed you used this code to point domain.com to /new/ folder, BOTH domain.com and domain.com/new/ will now show you exactly the same content but with two different URLs (this will cause duplicate content issue). I tried fixing this using 301 redirect in another .htaccess file in the /new/ folder but it didn't work. Doing this causes the redirection to loop around and the website will not be accessible. One viable solution is to be consistent in your internal linking by using the form domain.com/page.htm consistently and never the domain.com/new/page.htm (search engine won't be able to crawl a URL they cannot see and visitors are unlikely to guess the name of the folder). The other solution is to use the parameter handling in Google Webmaster Tools, but this does not cover the URLs that Yahoo and Bing can crawl.
  • Everything that was said in the previous bullet also applies to two domains wherein one domain points to a folder. This time however, there are three URLs from where a site can be accessed: domain2.com, domain2.com/new/ and domain.com/new/.
  • The code does not come in conflict with the WordPress inserted code for permalinks in the same .htaccess. but how about the other codes that we can insert in the .htaccess? I haven't yet to test them.
  • In one of my client website being hosted at GoDaddy, I tried this by installing WordPress at domain.com/site2/ while there's another WordPress installed for an earlier site in the root directory of domain.com. Both domain.com and domain2.com points to the root directory. I use this code to point domain2.com at /site2/ folder. While WordPress works as expected for default and classic theme, the theme that I uploaded (newtheme) doesn't work. The CSS cannot be accessed through domain2.com/wp-content/themes/newtheme/style.css. The folder where uploaded images are being saved are not accessible either despite being at the server as can be seen in the file manager. This requires me further research.

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://codegrad.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





Comment Rules and Reminders

  • The links to the commentator's e-mail do not have nofollow tag. However, I will be very strict in approving comments.
  • When you comment, please say something that indicates that you indeed read my post. If your comment is a general statement that can fit to any blog post about any topic, it will be regarded as spam.
  • What you write in the name field may include keywords to your website provided that (1) it's only up to four words long and (2) at least one of these four words is your first name or nickname. I rather reply to Bob or to Joe Smith than to Online Marketing Tips.
  • 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.
  • Fields marked with asterisks (*) are required. Your email will never be displayed in public.