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 / WebmasterWorld / New To Web Development
Forum Library, Charter, Moderators: brotherhood of lan & mack

New To Web Development Forum

Capitalization in changing /index.htm to /
Is there code where caps can also be redirected?

 5:59 am on May 7, 2012 (gmt 0)

Hello all,

I am a newcomer to WebmasterWorld and to web design in general, especially the behind-the-scenes things that seem to be so important in modern websites. I webmaster our website (a small religious non-profit) for lack of anyone better, lol, so I try to do my best.

Recently we ran into a problem that led to me learning for the first time about .htaccess and how useful it is. After a lot of reading/research, I finally ended with an .htaccess file that does, among other things:

- 301 redirect index.htm and index.html to the folder base /
- 301 redirect www.example.com to example.com

The code I ended up using for these came from a really amazing post on this site: [webmasterworld.com ] - my especial thanks to g1smd, his final post on that thread was superb and became the exact base I used for my code.

However, in testing, i discovered that although the code was designed to cover and work for many different possibilities, it does not work for capital letters. What I mean is, index.htm is correctly being redirected, but not Index.htm or INDEX.HTM or index.HTM etc. Now I know that it is not best practices to have strange capitalizations and such, but that is a battle for another time, and I have to deal with what I have. Also I want it to redirect correctly even if a visitor types in or links an index file wrong with some caps in it.

So I am asking, is there a way to further strengthen the code to also cover this possibility? If there is, maybe we can post that entire code here as an even further expanded iteration of the previous one, for future noobs to be helped by!

Here is the code I am currently using, as copied from that post (the only difference is that post was designed to make non-www into www, and I switched it the other way around):

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

RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]

Thanks in advance for your time and assistance!




 6:29 am on May 7, 2012 (gmt 0)

Every change, however subtle, is intended:

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

RewriteCond %{HTTP_HOST} !^(example\.com)?$
RewriteRule (.*) http://example.com/$1 [R=301,L]

There were ten (not including using "example" dot com here).

There were flaws in some of the code I posted here six years ago.

[edited by: brotherhood_of_LAN at 10:35 pm (utc) on May 7, 2012]
[edit reason] updated code on poster's request [/edit]


 7:49 am on May 7, 2012 (gmt 0)

welcome to WebmasterWorld, Noor!

i think this is what you want:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html? [NC]
RewriteRule ^(([^/]+/)*)index\.html?$ http://example.com/$1 [R=301,L,NC]

RewriteCond %{HTTP_HOST} !^(example\.com)?$
RewriteRule (.*) http://example.com/$1 [R=301,L]

the key change made by g1smd was the [NC] flag in the first RewriteRule and i further edited that rule to redirect to the non-www on the first pass.


 8:46 pm on May 7, 2012 (gmt 0)

Thank you both for your replies. I have implemented the code that you suggested, and gave it a day to make sure it took effect, but I am sorry to say that the problem is still not resolved. Any version of index.htm with capital letters is not redirecting correctly.

I wonder if some of the rest of my .htaccess code might be messing it up or interfering? I will post everything I have in it:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html? [NC]
RewriteRule ^(([^/]+/)*)index\.html?$ http://example.com/$1 [R=301,L,NC]
RewriteCond %{HTTP_HOST} !^(example\.com)?$
RewriteRule (.*) http://example.com/$1 [R=301,L]
RewriteCond %{SERVER_PORT} =443
RewriteCond %{REQUEST_URI} !folder/
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
RewriteCond %{SERVER_PORT} ^443$
RewriteRule ^robots.txt$ robots_ssl.txt

As far as I understand it, this is what all of these do:
- First three lines are necessary to make the rest work
- Next two lines redirect index.htm to folder base
- Next two lines redirect www to non-www
- Next three lines redirect https to http, with an exception for one folder where our e-commerce is (although the exception is not working right now either, I have tried several iterations with no success, this one I am currently using I found here: [webmasterworld.com ])
- Last two lines work with special robots_ssl.txt file to disallow any further spidering of https pages

Further information, the site is hosted on IIS 6 with ISAPI Rewrite enabled, and our hosting company's tech support also told me that REQUEST_URI should work with them (I read that for some using IIS/ISAPI it doesn't).

Again, appreciate any help you might be able to provide. Thanks very much.


 9:05 pm on May 7, 2012 (gmt 0)

Ha! I still manged to include a typo. Thanks for the correction.

The code should work on Apache. For IIS I am not so sure.


 2:02 am on May 8, 2012 (gmt 0)

IIS must be specifically configured to be case-sensitive.
out of the box it is case-insensitive.
i'm not sure how this setting affects the usage of the NC flag.


 2:25 am on May 8, 2012 (gmt 0)


RewriteCond {see below}
RewriteRule [A-Z] /foobar.html [R=301,L]

For your RewriteCond, put anything that will constrain the rule to yourself: your IP address, some unusual feature of your UA, etc. In the Rule, do not include [NC]. For the target, make up any filename that doesn't really exist. You will end up seeing your 404 page, but the address bar will say "foobar.html" or whatever silly name you decide to use.

Request anything containing a capital letter, whether it really exists or not. You should get redirected. Request anything not containing a capital letter. You should not get redirected.

This is purely diagnostic to make sure your server can identify casing.


 8:07 am on May 8, 2012 (gmt 0)

.htaccess changes should be effective immediately, so there's no need to sleep on it before testing.

are you clearing browser cache before testing each change of .htaccess?


 4:15 am on May 17, 2012 (gmt 0)

Thanks everyone for your replies and help. I have found the reason it was not working was due to a human error so foolish I do not want to even say it, lol. But your code indeed works fine. If any other coding rookies find this page, use the code given by g1smd or phranque above, it is sound. Thanks again!


 5:51 am on May 17, 2012 (gmt 0)

We have all made those errors, and continue to make those errors.

Global Options:
 top home search open messages active posts  

Home / Forums Index / WebmasterWorld / New To Web Development
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