Changing the top level directory with .htaccess files
When our Web servers receive a request for “http://www.example.com/” or “http://example.com/”, they show the index file from the top level directory of your website.
Occasionally, people want to use a different directory as the top level (aka “document root”). For example, you might want the contents of your “blog” directory to show as the main page.
The simplest way to do that is to move the contents of the “blog” directory to the top level. That’s the way we recommend doing it. If it’s done a different way, we’ve found that someone often contacts us later, not understanding why the top level doesn’t work as they expect — frequently after a new person takes over the site management.
If you still want to do this without moving files, the page below explains how.
Changing how the Web server handles the top level
If you can’t move the files for some reason, technically advanced customers can use a .htaccess file at the top level of the site to “rewrite” the top level requests.
This .htaccess example shows how to make a directory named “subdir” act as the top level:
RewriteEngine On RewriteCond %{REQUEST_URI} !^/subdir/ RewriteRule ^(.*) /subdir/$1 [L]
The “RewriteCond” line is crucial: it makes sure that any existing URLs that begin with “/subdir” continue to work, and more importantly, it prevents an infinite loop of rewriting.
The example above will modify requests for any page that doesn’t already begin with “/subdir/”. If you have other existing directories that need to keep working, you can include those as additional directories to leave alone. This example will also allow URLs beginning with “/archives” and “/images” to keep working without any rewriting:
RewriteEngine On RewriteCond %{REQUEST_URI} !^/subdir/ RewriteCond %{REQUEST_URI} !^/archives/ RewriteCond %{REQUEST_URI} !^/images/ RewriteRule ^(.*) /subdir/$1 [L]
And this example shows how to avoid affecting a subdomain like “test.example.com”:
RewriteEngine On RewriteCond %{REQUEST_URI} !^/subdir/ RewriteCond %{HTTP_HOST} !^test.example.com [nocase] RewriteRule ^(.*) /subdir/$1 [L]
Can I “preview” a site in a directory before making it visible to everyone?
You can add additional “conditions” to make this feature only visible to some people, which is useful for testing before you make the change “live”.
For example, you can make it happen only for people connecting from certain IP addresses:
RewriteEngine On RewriteCond %{REMOTE_ADDR} ^3.147.62.5$ RewriteCond %{REQUEST_URI} !^/subdir/ RewriteRule ^(.*) /subdir/$1 [L]
Or you can make it happen only to visitors who have added a custom HTTP header in their browser. For example, this looks for a header named “X-PREVIEW-NEW-SITE” with a value of “1”:
RewriteEngine On RewriteCond %{HTTP:X-PREVIEW-NEW-SITE} 1 RewriteCond %{REQUEST_URI} !^/subdir/ RewriteRule ^(.*) /subdir/$1 [L]
A caveat about “favicon.ico” files
Due to a technical quirk, this won’t correctly use a “favicon.ico” file in your special directory if there’s no “favicon.ico” at the top level, too. To avoid this problem, just make sure there’s a file named “favicon.ico” at the top level of the site (the contents don’t matter and won’t actually be used).
The same thing applies to Apple’s special “favicon.ico” equivalent files named “apple-touch-icon.png”, “apple-touch-icon-precomposed.png”, and so on.
Need help?
If you’re having trouble, contact us with the details of what you want to do. We’ll be glad to help you find the best solution for your situation.
Copyright © 2000-2024 Tiger Technologies LLC