Welcome to WebmasterWorld Guest from 54.197.116.116

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Redirect Both index.htm and index.html to Root

   
1:55 am on Oct 3, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've just taken over the site from hell.

Not only does it have a structure that contains numerous "/dir/index.htm" files, it also has lots of external incoming links to both "index.htm" and "index.html."

I've captured 700+ of the incoming links to "index.htm" with this...

RewriteEngine On
RewriteRule ^index.htm$ / [R=301,L]

..but now I'm trying to also capture the links coming into "index.html," but nothing is working. Everything I try winds up in a loop.

Does anyone see a way to modify the above rule to also redirect "index.html" to "/"?

<added>

I've read numerous older posts on the subject in this forum, and one in particular from JD leds me to believe that it can't be done without first getting rid of all the "/dir/index.htm" files.
2:11 am on Oct 3, 2012 (gmt 0)

WebmasterWorld Senior Member



RewriteEngine On
RewriteRule ^index.html? / [R=301,L]

Maybe, just tried it here and works. Only added an "l" and ? and removed your $
2:45 am on Oct 3, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey, it works! I could have sworn that I tried that one previously.

Thanks much.
2:59 am on Oct 3, 2012 (gmt 0)

WebmasterWorld Senior Member



Glad to hear that. Now maybe you can do me a favour. Seeing as you are in the site from hell can you look around for any ancient texts that might be of use to us up here. Apparently the secrets of heaven are hidden in the pits of hell. Kinda cool when you think of it, who would ever want to go there looking for them.
4:17 am on Oct 3, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The only ancient text on this dog is reams and reams of <font> tags...

(...[sigh]...oh the things we do for a PR6 link...)
6:26 am on Oct 3, 2012 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Your code now redirects requests for
example.com/index.html
to
example.com/
and requests for
www.example.com/index.html
to
www.example.com/


If you also have a non-www to www rule in your htaccess file, a request for
example.com/index.html
is redirected twice before eventually reaching
www.example.com/
- use the Live HTTP Headers extension for Firefox to confirm this.

This unwanted redirection chain will cause issues.

You should add the canonical protocol and hostname to the rule above and make sure that the index redirect is listed before the non-www/www redirect in the htaccess file.

You should also escape all literal periods in patterns.

If your site actually uses an index.html or index.htm file to serve content you probably can't view the content now and get an error message instead. You will need to add a preceding
RewriteCond
looking at
THE_REQUEST
to ensure that only external requests for the index file are redirected, not requests for the index file that happen as a result of the final internal rewrite to fetch the content.

Finally, why redirect only root requests? Using
^(([^/]+/)*)index\.html?$
will enable this redirect for all folder levels.

Example code for this appears in thousands of previous threads. Note: If there is a (.*) pattern in the index rule then it is the wrong code.
1:25 pm on Oct 3, 2012 (gmt 0)

WebmasterWorld Senior Member



Ya, what g1smd said.

I should have known my suggestion was too easy to be as efficient as the one pointed out above. But, I want to learn this stuff too so it becomes second nature so I want to participate in these types of posts. It will reinforce my understanding until I can speak regex confidently. Right now I only deal with it a couple times a year and it's usually a painful chore.
4:26 pm on Oct 3, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I just ran several url variations through a header checker and you are correct, non-www urls do a double redirect.

More reading to do...
6:07 pm on Oct 3, 2012 (gmt 0)

WebmasterWorld Senior Member



Besides what I said above I also tend to just hyper-focus on the exact problem presented and assume the OP is already aware and/or has other concerns taken care of -- at least here in Apache because my knowledge is limited. That's the opposite to other threads where I can easily wonder off topic expanding on the OP because my monkey-mind just likes to go wandering at times -- like right now for instance :P
6:26 pm on Oct 3, 2012 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Most htaccess files will have three groups of code:
- rules that block access
- rules that redirect
- rules that internally rewrite.

You need a RewriteCond + RewriteRule pair to deal with index requests then a separate pair to deal with non-www/www requests. Both RewriteRules should have the canonical hostname in the rule target. This avoids the double redirect.

Generally you should order rules from most specific to most general within each of the three groups, then only one rule will match a request. The [L] flag stops the rest of the rules being tested after one has already matched.
3:12 am on Oct 5, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks g1smd.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month