Reading time: 1 – 2 minutes

This is only a quick Apache tip for when you are using mod_rewrite.

I’ve been working on some rewriting lately, and noticed that when you use them, the pattern applied must match exactly, otherwise you will either get error, or your pattern will never find a match.

Obviously you have a few options when writing your rewrite rules to make it case insensitive, but that means you will have to use it on every single rule.

In my case, I simply want everything to come though as lower case, so even if you hit any of my pages with an upper case URL, it will automatically be rewritten to the same thing, but in lower case, and return 301, so the search engines know the page was permanently moved.

This is how I do it:

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

The RewriteMap directive needs to be placed on your httpd.conf or VHOST, as it won’t work on directory level or .htaccess. Everything else can go on your directory or .htaccess.

Share and Enjoy:
  • Twitter
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • del.icio.us
  • Digg
  • Slashdot
  • Technorati
  • Posterous
  • LinkedIn
  • Reddit

3 Responses to “Apache 101 – Case insensitive URL’s”

  1. johnb says:

    the -NC option on your rule should make it case insensitive

  2. Neil Middleton says:

    And the reason for wanting to do this is?

  3. johnb says:

    ah, I think I missed the point of your posting.
    I’ll shutup

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>