homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Redirect Both index.htm and index.html to Root

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

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 "/"?


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)

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)

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

Thanks much.


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

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)

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)

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)

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)

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)

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)

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)

Thanks g1smd.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved