Welcome to WebmasterWorld Guest from 23.22.79.235

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Redirecting index.html to /

Can't make it work in subdirectories

   
12:11 pm on Jan 7, 2007 (gmt 0)

5+ Year Member



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?)

6:17 pm on Jan 7, 2007 (gmt 0)

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



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.

Jim

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