Forum Moderators: phranque

Message Too Old, No Replies

301 Redirect Help Needed

         

pro_seo

5:23 am on Aug 30, 2007 (gmt 0)

10+ Year Member



Hello Friends,

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]

jdMorgan

1:11 pm on Aug 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One of my favorite topics... :)

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]

The RewriteCond and RewriteRule patterns will match
example.com/index.html or
example.com/foo/index.html or
example.com/foo/bar/index.html or
example.com/foo/bar/.../quux/index.html
or any other URL where "index.html" is requested in any subdirectory,

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

pro_seo

6:04 pm on Aug 30, 2007 (gmt 0)

10+ Year Member



That was awesome Jim..I will try out with the code that you've provided.

Thanks a ton for that mate :)

pro_seo

4:55 am on Aug 31, 2007 (gmt 0)

10+ Year Member



Hi Jim,

Just dropping by to say that the code you provided works perfectly...thanks a lot for that :)

g1smd

6:47 pm on Sep 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Another thread that should be listed in the "best rewrite resources" list.

Good explanation.