homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Redirecting index.html to /
Can't make it work in subdirectories

5+ Year Member

Msg#: 3211162 posted 12:11 pm on Jan 7, 2007 (gmt 0)

Having first studied documentation and several older threads here I combined the information into the following lines:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^(.*)index\.html$ [%{HTTP_HOST}...] [R=301,L]

but, sadly, I can only make this work in the root directory. To the subdirectories' index files the code does nothing at all. Any help?

(I am using that HTTP_HOST because there are two domains controlled by my .htaccess. Maybe this piece of code simply will not work in such a set up?)



WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member

Msg#: 3211162 posted 6:17 pm on Jan 7, 2007 (gmt 0)

You just need to allow for zero or more directory paths in both the RewriteCond and RewriteRule, otherwise, they won't match subdirectory index requests:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html$ http://%{HTTP_HOST}/$1 [R=301,L]

I have used "([^/]+/)*" as the pattern, which means, "Match one or more characters not equal to a slash, followed by a slash, and and many instances of that sequence as you like (zero or more subdirectory levels)." Unlike using ".*", this pattern can be processed from left to right in a single pass of the pattern-matcher, and is therefore more efficient.

Try to avoid the use of the ".*" pattern. It is "easy" to use, but can be grossly inefficient, especially when it occurs more than once in a pattern.


[edited by: jdMorgan at 6:17 pm (utc) on Jan. 7, 2007]

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved