Force Lower-Case URLs with mod_rewrite

You may find yourself in a situation at some point where you need to ensure that all access to your site is done using lower-case only. This may be because of SEO (avoiding duplicate content), or perhaps you want to ensure that you can seamlessly move between case-sensitive/insensitive operating systems. If you are running a standard LAMP stack, you’re in luck. Apache’s mod_rewrite can be used to 301-redirect all incoming requests to their lower-case counterparts with just a few configuration directives.

10. Don’t be Case Sensitive
Since URLs can accept both uppercase and lowercase characters, don’t ever, ever allow any uppercase letters in your structure. If you have them now, 301 them to all-lowercase versions to help avoid confusion. If you have a lot of type-in traffic, you might even consider a 301 rule that sends any incorrect capitalization permutation to its rightful home.
http://www.seomoz.org/blog/11-best-practices-for-urls

It doesn’t require much to rewrite URLs to lower-case, and the necessary configuration directives can be placed in either a virtualhost or .htaccess file. First, the RewriteEngine is enabled, and a custom RewriteMap called ‘lc’ is defined using the internal ‘tolower’ map. Next, a RewriteCond rule is used so that only URLs containing upper-case characters are rewritten. Finally, the actual RewriteRule is defined using the ‘lc’ RewriteMap that we just created to transform the captured URL to lower-case before 301-redirecting the user.

RewriteEngine On
RewriteMap  lc int:tolower
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule (.*) ${lc:$1} [R=301,L]

The following is the output from the RewriteLog that shows the processing for a request to /INDEX.PHP. Note that only a single upper-case letter needs to be present for the rewrite rule to be triggered. The log output clearly shows the rewrite rule being applied, the RewriteMap being referenced, and the 301 redirect taking place. The subsequent request for /index.php is also shown, where the rewrite condition does not match and the request is passed through.

init rewrite engine with requested uri /INDEX.PHP
applying pattern '(.*)' to uri '/INDEX.PHP'
RewriteCond: input='/INDEX.PHP' pattern='[A-Z]' => matched
map lookup OK: map=lc key=/index.php -> val=/index.php
rewrite '/INDEX.PHP' -> '/index.php'
explicitly forcing redirect with http://test/index.php
escaping http://test/index.php for redirect
redirect to http://test/index.php [REDIRECT/301]
init rewrite engine with requested uri /index.php
applying pattern '(.*)' to uri '/index.php'
RewriteCond: input='/index.php' pattern='[A-Z]' => not-matched
pass through /index.php
Chris Abernethy
PHP Wrangler, MySQL DBA, Linux SysAdmin and all around computer guy, developing LAMP applications since Slackware came on 10 floppy disks.

27 Comments on "Force Lower-Case URLs with mod_rewrite"

  1. Ratnesh says:

    Hi there,
    I am having problem with the above code its not working on local servers.(WAMP)
    The error it gives is internal server error.
    Or
    where should I place this code
    I thinks in the

    RewriteEngine On
    RewriteMap lc int:tolower
    RewriteCond %{REQUEST_URI} [A-Z]
    RewriteRule (.*) ${lc:$1} [R=301,L]

    Please help me out.

  2. Steve says:

    I’m getting the internal server error as well. I suspect that it has to do with the configuration of the servers. Specifically: “Due to the way in which our server environments are setup you may not use php_value arguments in a .htaccess file.”

  3. Hello, its fastidious article on the topic of media print, we all be aware of media is a wonderful source of facts.

  4. Kenan says:

    @Ratnesh for localhost you need to open rewrite extension.

    Also i did it for my web site but nothing changed . It shows again uppercase.

  5. Reflexologías aplicadas al quiromasaje Personas que quieran ampliar
    y enriquecer su desarrollo personal y aplicar el masaje en su entorno familiar.
    MASAJE TAILANDES TRADICIONAL sesenta ´ Información y
    cita previa al 616 55 99 veinticinco Historia del masaje..

  6. Actually when someone doesn’t understand afterward its up to other visitors that they will help, so here it occurs.

  7. Usually I don’t read post on blogs, however I would like to say that this write-up very forced
    me to try and do so! Your writing taste has been amazed me.
    Thanks, very great article.

  8. Josette says:

    Excellent post. I used to be checking continuously this weblog
    and I’m impressed! Very useful information specially the ultimate phase :)
    I handle such information much. I was looking for this certain info for a very lengthy time.
    Thank you and good luck.

  9. I am really loving the theme/design of your web
    site. Do you ever run into any web browser compatibility issues?

    A few of my blog readers have complained about my website not working correctly in Explorer but looks great in Safari.
    Do you have any advice to help fix this problem?

  10. This is the right webpage for anybody who wishes to understand this topic.
    You understand so much its almost tough to argue with you (not that
    I actually would want to…HaHa). You definitely put a new spin on a topic which has been written about for many years.
    Great stuff, just excellent!

  11. Reto90 says:

    Hi there Dear, are you actually visiting this web page daily, if
    so then you will without doubt take pleasant knowledge.

  12. First of all I want to say terrific blog!

    I had a quick question in which I’d like to ask if you don’t mind.

    I was interested to know how you center yourself and clear your mind before writing.
    I’ve had trouble clearing my mind in getting my ideas
    out there. I do take pleasure in writing however it just seems like the first
    10 to 15 minutes are usually wasted just trying to figure
    out how to begin. Any ideas or tips? Many thanks!

  13. Very good blog post. I absolutely appreciate this website.
    Keep writing!

  14. It is in reality a nice and helpful piece of info.

    I’m satisfied that you just shared this useful info with us.

    Please stay us informed like this. Thank you for sharing.

  15. It’s really a cool and helpful piece of info. I’m glad that you simply shared this helpful information with us.
    Please stay us up to date like this. Thank you for sharing.

  16. Ytb is the only mlm firm that offers you without spending a dime,
    INSURANCE COVERAGE ; AS IN; MEDICAL, DENTAL, IMAGINATIVE AND PRESCIENT, LIFE,
    . a long time upfront as a result of data about it was gathered through
    DARPA’s secret time travel program unlocks a number of
    of the extra enigmatic details within the Mr. Basiago has publicly answered
    questions about this corroborating photographic proof of his time travel experiences within the secret U.S.
    government program during which time travel was achieved.

  17. Iphone four GEVEY Sim Adapter FAQ:
    If your Iphone 4 has customized firmware, the GEVEY Sim adapter will not get the job done.
    You must restore your Iphone 4 to the original Apple iphone four
    Firmware.

  18. hosei-im.jp says:

    I wanted to thank yyou for this great read!! I definitely loved every
    bit of it. I have you book marked to look at new things you post…

  19. web page says:

    Attractive portion of content. I simply stumbled upon your weblog and in accession capital to assert that I get actually loved account your blog posts.
    Any way I will be subscribing to your feeds and even I success you get
    entry to constantly fast.

  20. agen tangkas says:

    I am genuinely glad to glance at this weblog posts which carries lots of valuable facts,
    thanks for providing such data.

  21. I read this post completely on tthe topic of the resemblance of most recent and preceding technologies,
    it’s amazing article.

  22. Why do not we just examples of the Prophet Muhammad, rather than looking for
    money that is not kosher as stealing, robbing, and corruption. Of these converts, the most qualified
    became “sheikhs”, as they were reverently called,
    and in various cities began study circles, gaining converts by the dozens, especially in the Midwest.
    The UK is served by two channel on the SKY network.

  23. automatem says:

    wonderful put up, very informative. I wonder why the other specialists of
    this sector don’t understand this. You should continue your writing.

    I am confident, you have a great readers’ base already!

  24. Have you ever considered writing an e-book oor guest authoring onn other blogs?

    I have a blog centered on the same information you discuss aand would really like
    tto have you suare some stories/information. I know my audience would value your work.
    If you’re even remotely interested, feel free to send me an e mail.

  25. aatarot.com says:

    The remaining cards (56 cards) are referred to as the Minor Arcana.
    Some people find this particularly uncomfortable due their off-putting associations.

    Horoscope is an established branch of science that takes into account planetary positions, person’s zodiac sign and date of
    birth.

  26. Besides teaching at Harvard, this man does consulting for
    Microsoft, the NFL, the New York Times and Universal Music on ‘preventing and detecting online fraud.
    The best way to find a best Indian restaurant in your town is to
    read the reviews and check the ratings of the restaurant online.
    Some major institutes providing this degree includes Ashford University, Columbia
    Southern University, South University, Beckfield College, Kaplan University, Pittsburgh Technical Institute, University of Phoenix, Vancouver Career college , Strayer University and YTI Career Institute.

  27. Hello my family member! I want to say that this article is
    amazing, great written and include approximately all vital infos.
    I would like to see extra posts like this .

Got something to say? Go for it!