FREE THOUGHT · FREE SOFTWARE · FREE WORLD

Home  »  Htaccess  »  Htaccess Rewrite for Redirecting Uppercase to Lowercase

by 11 comments

Want to redirect all links with any uppercase characters to lowercase using pure mod_rewrite within an .htaccess file? Sure why not! OR how to use RewriteMap and mod_speling for those with access to httpd.conf


Htaccess to Redirect Uppercase to Lowercase

This should go at the very top of your .htaccess file. At least it should go above ANY other RewriteRules. That is because this uses a loop, until there are no more uppercase characters to convert, it will keep starting at the first HASCAPS:TRUE RewriteRule. Oh, and this is actually really quick and isn't gonna slow down anything.

RewriteEngine On
RewriteBase /

# If there are caps, set HASCAPS to true and skip next rule
RewriteRule [A-Z] - [E=HASCAPS:TRUE,S=1]

# Skip this entire section if no uppercase letters in requested URL
RewriteRule ![A-Z] - [S=28]

# Replace single occurance of CAP with cap, then process next Rule.
RewriteRule ^([^A]*)A(.*)$ $1a$2
RewriteRule ^([^B]*)B(.*)$ $1b$2
RewriteRule ^([^C]*)C(.*)$ $1c$2
RewriteRule ^([^D]*)D(.*)$ $1d$2
RewriteRule ^([^E]*)E(.*)$ $1e$2
RewriteRule ^([^F]*)F(.*)$ $1f$2
RewriteRule ^([^G]*)G(.*)$ $1g$2
RewriteRule ^([^H]*)H(.*)$ $1h$2
RewriteRule ^([^I]*)I(.*)$ $1i$2
RewriteRule ^([^J]*)J(.*)$ $1j$2
RewriteRule ^([^K]*)K(.*)$ $1k$2
RewriteRule ^([^L]*)L(.*)$ $1l$2
RewriteRule ^([^M]*)M(.*)$ $1m$2
RewriteRule ^([^N]*)N(.*)$ $1n$2
RewriteRule ^([^O]*)O(.*)$ $1o$2
RewriteRule ^([^P]*)P(.*)$ $1p$2
RewriteRule ^([^Q]*)Q(.*)$ $1q$2
RewriteRule ^([^R]*)R(.*)$ $1r$2
RewriteRule ^([^S]*)S(.*)$ $1s$2
RewriteRule ^([^T]*)T(.*)$ $1t$2
RewriteRule ^([^U]*)U(.*)$ $1u$2
RewriteRule ^([^V]*)V(.*)$ $1v$2
RewriteRule ^([^W]*)W(.*)$ $1w$2
RewriteRule ^([^X]*)X(.*)$ $1x$2
RewriteRule ^([^Y]*)Y(.*)$ $1y$2
RewriteRule ^([^Z]*)Z(.*)$ $1z$2

# If there are any uppercase letters, restart at very first RewriteRule in file.
RewriteRule [A-Z] - [N]

RewriteCond %{ENV:HASCAPS} TRUE
RewriteRule ^/?(.*) /$1 [R=301,L]

Using RewriteMap in httpd.conf

This is technically a faster way to do this, but it has to be in the httpd.conf file, not .htaccess

RewriteEngine on
RewriteBase /
RewriteMap lowercase int:tolower
RewriteCond $1 [A-Z]
RewriteRule ^/?(.*)$ /${lowercase:$1} [R=301,L]

Using mod_speling in httpd.conf

You can check out enabling the mod_speling apache module also. I personally don't use it, but many people love it.


CheckCaseOnly On
CheckSpelling On

Tags

April 30th, 2013

Comments Welcome

  • Ashish

    Takes forever when I try the .htaccess method on xampp

  • Ashish

    Takes forever when I try the .htaccess method on xampp

  • CoreT

    Do you know of anyway to force lower case URL's but not affect the capitalisation of adwords auto tagging? When you force the URLs lowercase the gclid paramater also changes to lower case and not all the data feed through to analytics? Thanks

  • Drunkard100

    I've tried the first method titled "Htaccess to Redirect Uppercase to Lowercase ^" It works, but adds superfluous "/" characters after the URL, resulting in a 404. For example, the URL http://example.com/UPPERCASE/ is rewritten to http://example.com/uppercase//////////. However, http://example.com/UPPERCASE is rewritten correctly without the superfluous "/" characters. I'm guessing the loop keeps adding the characters after each letter rewrite.

  • http://www.scottrobertsweb.com Scott Roberts

    I would definitely NOT try the first example where you use the .htaccess file. It causes server resources to skyrocket and bog down the server. I tried using this approach, and it made my web host temporarily suspend my account until we were able to pinpoint that this was indeed the problem and I removed it.

    I would say that it is a remote option if your website traffic is 10-15 visitors per day, but if you have any substantial traffic it is not the solution for you. So the "Oh, and this is actually really quick and isn't gonna slow down anything." line from above is very misleading.

  • Vimlesh Maurya

    Thanks a lot for helping us. You have very deep knowledge of .htaccess.

    Thanks

  • LetsMixx

    There is no "mod_speling" in my httpd.conf ! what to do ?

    just write this

    CheckCaseOnly On
    CheckSpelling On

    in some place of httpd.conf ?

    I am new to this :/

  • Richard Noble

    This does not work for GoDaddy sites. Does anyone know of a working solution for GD?

  • oswald

    DO NOT USE THIS CODE or you will DOS your own box. As mentioned below, its BUGGED.

    • http://www.scottrobertsweb.com Scott Roberts

      It doesn't really do a DDoS-style attack, which are meant to completely take your site down. Your site will definitely stay up and running, but it will be much slower, and your server resources will increase multiple times it's current load.

      Since I was running a Wordpress site, I found a plugin that fixed this without using the modified .htaccess code found here.

      • oswald

        It will cause a loop when an uppercase is the first letter after a / I believe. These .htaccess rules were crashing a monster server with 32GIG of ram after running for a couple of minutes.
        I switched to a different method of doing upper to lower case and these urls no longer create apache process that utilize 100% of the ram.
        I tracked the server crashes down to these processes and this particular set of .htaccess rules 100%.
        the modified .htaccess syntax is a case insensitive match.
        RedirectMatch 301 (?i)/originurl redirecturl


Related Articles


My Online Tools
Popular Articles


Hacking and Hackers

The use of "hacker" to mean "security breaker" is a confusion on the part of the mass media. We hackers refuse to recognize that meaning, and continue using the word to mean someone who loves to program, someone who enjoys playful cleverness, or the combination of the two. See my article, On Hacking.
-- Richard M. Stallman









[hide]

It's very simple - you read the protocol and write the code. -Bill Joy

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 License, just credit with a link.
This site is not supported or endorsed by The Apache Software Foundation (ASF). All software and documentation produced by The ASF is licensed. "Apache" is a trademark of The ASF. NCSA HTTPd.
UNIX ® is a registered Trademark of The Open Group. POSIX ® is a registered Trademark of The IEEE.

+Askapache | askapache

Site Map | Contact Webmaster | License and Disclaimer | Terms of Service

↑ TOPMain