| 2:11 am on Oct 3, 2012 (gmt 0)|
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.
| 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/ and requests for
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)|