Forum Moderators: phranque
I have a domain for which I've set up a 301 redirect which redirects /index.htm to /
Now the problem is that the domain also has some pages like /abc-index.htm ; /xyz-index.htm
Now while the redirection is working fine for /index.htm to / it is truncating the index.htm portion of /abc-index.htm and others causing a 404 error.
I am using this code for the redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.html\ HTTP/
RewriteRule ^(.*)index\.html$ /$1 [R=301,L]
Kindly let me know what to do if I want to redirect www.example.com/index.htm to www.example.com/ while keeping the others intact like www.example.com/abc-index.htm
Thanks a lot in advance
P.S - Sorry JD, I am unable to remove the live links of domain.com
[edited by: jdMorgan at 12:47 pm (utc) on Aug. 30, 2007]
[edit reason] example.com [/edit]
The ".*" pattern is very easy to understand and simple to use, but can be very inefficient in many cases. It is also greedy and promiscuous -- It will match any characters, and as many of them as possible. So its use often results in problems like this.
The cure is to use a more-specific pattern, so that the pattern only matches if the "index.html" is preceded directly by a slash. We also need to be sure that we don't drop the directory-path, especially in the case where the index file is several directories deep.
So, a good pattern to use is "[^/]+/" -- That is, "match one or more characters not equal to a slash, followed directly by a slash."
Enclosing that in parentheses and adding a quantifier to form "([^/]+/)*" gives us, "match one or more characters not equal to a slash, followed directly by a slash," and as many of those sequences as you like -- zero or more.
Finally, in order to make sure that all such sequences can be back-referenced as the single variable "$1", we need to enclose this pattern (in the RewriteRule only) with an outer set of parentheses. So the result is:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html$ /$1 [R=301,L]
But they won't match
example.com/abc-index.html or
example.com/foo/123index.html or
example.com/foo/bar/x-index.html or
example.com/foo/bar/.../quux/newindex.html
or any other URL where "index.html" is not directly preceded by slash.
Again, good advice for all mod_rewrite users: Make your regular-expressions patterns as specific as possible, and you won't have this problem. If in doubt, make your patterns too specific and then relax them only as necessary to match all of the URL-paths you need to match.
Jim