Marcos Placona Blog

Programming, technology and the taming of the web.

Apache 101 – Case insensitive URL’s

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.

9 Comments

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

  2. Neil Middleton

    May 22, 2009 at 00:00

    And the reason for wanting to do this is?

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

  4. When I tried this I got a “Internal Server Error” message.

  5. Marcos Placona

    January 31, 2011 at 09:22

    Can you send me the exact change you’ve made, and make sure you have mod_rewrite running and enabled on your server? Also, this change needs to be on the top of the file (.htaccess or whatever you’re using to add your directives)

  6. Ok, I pleased this code in my httpd.conf file and nothing happens… my url is still domain.com/Page/Page-1/ and domain.com/page/Page-1/ etc etc.

  7. Marcos Placona

    January 31, 2011 at 14:44

    Try replacing the line:

    RewriteCond %{REQUEST_URI} [A-Z]

    With:

    RewriteCond %{REQUEST_URI} [A-Z0-9\-]

    And see if it helps.

    Cheers

  8. I added “RewriteMap  lc int:tolower” to my vhost file and restarted Apache. I then added:
    #Make URL’s lower case
    RewriteEngine On
    RewriteCond %{REQUEST_URI} [A-Z0-9\-]
    RewriteRule (.*) ${lc:$1} [R=301,L]
    to my .htaccess file, but sadly all my URLs are still in uppercase. For example: http://www.mysitedomain.com/East-Anglia-office-space-to-rent

    Any ideas why?

  9. I am wanting to use this to cause the context root /ap/pg to be case insensitive. However when I redirect I always want the redirect to be in lowercase.

    How can I modify this to just be for /ap/pg only for case insensitivity and not affect other context roots coming thru apache.

Leave a Reply

Your email address will not be published.

*

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