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.

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.

39 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.
    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,
    . 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

  18. 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. 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

  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 .

  28. Great post. I was checking constantly this blog and I’m impressed!

    Extremely useful information particularly the last part :) I
    care for such information a lot. I was seeking this particular info
    for a very long time. Thank you and good luck.

  29. Dessert ecosystem animals- Animals found in desert
    regions will be nocturnal carnivores. In coping with the
    loss of your pet, guilt can be one of the strongest emotions you
    experience. What follows is an overview of several of the most rare breeds of Persian cats:
    The fantastic white is most likely the most preferred wide variety of this unique

  30. Stephaine says:

    When someoe writes an paragraph he/she keepps the plan of a
    user in his/her brain that how a user can know it.

    So that’s why this article iis perfect. Thanks!

  31. Nice post. I learn something totally new and challenging on websites I stumbleupon every day.
    It’s always helpful to read articles from other authors and practice a
    little something from their websites.

  32. Ingeborg says:

    I used to be suggested this website by means of my cousin. I am no longer positive
    whether or not this put up is written by him as no one else know such targeted approximately my difficulty.
    You are wonderful! Thanks!

  33. Davida says:

    Hello just wanted to give you a quick heads up. The words in yokur
    post seem to be running off the screen in Internet explorer.
    I’m not sude iff thiis is a formatting issue or something to do with internet browser compatibility but I
    figured I’d post to let you know. The layout look great
    though! Hope you gget the problem fixed soon. Cheers

  34. says:

    Horny goat weed: When you’re developing a hard time in dealing with dull sex-life, than the herb will probably
    be your perfect option. Montys_Mom: It was a tough couple years
    when I had to live with my mother in-law. What’s the
    aim of losing two pounds in a week when you’ve
    got 120 to go.

  35. games says:

    This article will help the internet users for creating new webpage or
    even a blog from start to end.

  36. Nice weblog here! Additionally your website so much up very fast!
    What host are you the usage of? Can I am getting your affiliate hyperlink
    for your host? I wish my website loaded up as fast as yours lol

  37. fluconazole says:

    If you want to obtain much from this paragraph then you have
    to apply these strategies to your won weblog.

  38. We are going to choose the ten top recipe thought submissions and will send 3 samples of the chosen product to the successful foodies.

  39. Pirate Bay says:

    There it absolutely was revealed that the PSP’s future may look very gloomy out.

    The uncovering of dried fruits, grains and dried, salt preserved fish and meat are traced time for over seven thousand years to these first ancient
    civilizations. Just because school rates aren’t advertised, that doesn’t mean that they
    aren’t available.

Got something to say? Go for it!