Forum Moderators: phranque
to redirect the root to a subdirectory all you need to do is use this in your .htaccess file
RewriteEngine On
RewriteBase /
RewriteRule ^index.(.*)?$ http://example.com/subdirectory [r=301]
this will redirect any attempt at www.example.com/index.htm to www.example.com/subdirectory
So now all you need to do is create an index.htm file into your root and it will redirect.
[edited by: jdMorgan at 1:18 pm (utc) on April 7, 2009]
[edit reason] Tidied-up. Please see Terms of Service. [/edit]
RewriteEngine on
#
RewriteRule ^index\..+$ http://example.com/subdirectory/ [R=301,L]
Literal periods in regex patterns should be escaped; Otherwise, "." is taken to mean, "Match any single character here."
The file extension on the /index.xyz URL-path should be specified, or at least it should be required, as shown here. Using ambiguous patterns which can unexpectedly match URLs leaves traps that you may stumble into later.
No parentheses are required in the RewriteRule pattern, since no back-reference is needed in the RewriteRule substitution.
If a redirect or rewrite is to a subdirectory rather than a file, then the trailing slash should be included in the RewriteRule Substitution URL.
I recommend that the documented use of capitalization in directives and flags be followed exactly to prevent unexpected problems.
All RewriteRules should end with an [L] flag, unless you have a very good reason not to include it.
Taking a step back from the code itself to the bigger picture, I'd also recommend never redirecting a root index request to a subdirectory. If necessary, you can internally rewrite to a subdirectory in order to keep your file storage tidy or to conform with the 'standard installation' of an off-the-shelf software package. But unless that software requires it, there is no reason to expose the filepath used to store the files on the server to the client in the URL-path used on the Web to refer to those files; URLs and filepaths are not at all the same thing, and changing one does not mean that you must change the other. In fact, the main benefit of mod_rewrite is allowing the Webmaster to change the default URL-to-filename mapping on the server.
So, to make the URL example.com/index.php resolve to the server filepath /subdirectory/, you would simply use:
RewriteRule ^index\.php$ /subdirectory/ [L]
Jim
[edited by: jdMorgan at 1:17 pm (utc) on April 7, 2009]