Forum Moderators: phranque

Message Too Old, No Replies

Only redirect index and not other variations

How to redirect only index requests

         

CainIV

5:02 am on Apr 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello, I have largely implement htaccess code from assistance and reading this forum, and thanks everyone including JD for the help.

I have one page on our website that is inevitably being rewritten

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

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

The issue is that I have pages such as this:

mysite.com/bookindex.html

The code is causing those to not show anymore. I can see that the issue is that the condition asks for anything then index.html, so naturally this page would fit the condition. I tried directly using an absolute url as the condition, but that threw errors as well.

What happens is that the code then strips the index.html off of the url and I am left with mysite.com/book, which obviously returns 404.

Any help is appreciated.

jdMorgan

2:00 pm on Apr 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Using a more-specific pattern will allow solving the problem using a single rule for both ".htm" and ".html":

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

Here we are looking for "one or more characters not a slash, followed by a slash, and as many of those sequences as you like (including zero). In simple terms, this requires that the character immediately preceding "index.htm<l>" (if present) must be a slash, so that "bookindex.html" will not be redirected.

Because we are not using a back-reference to the RewriteCond pattern, only one level of parentheses is needed there, as opposed to the RewriteRule, where two levels are needed to capture the entire leading directory path (if present) for use in the substitution URL.

Jim