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.
the -NC option on your rule should make it case insensitive
And the reason for wanting to do this is?
ah, I think I missed the point of your posting.
I’ll shutup
When I tried this I got a “Internal Server Error” message.
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)
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.
Try replacing the line:
RewriteCond %{REQUEST_URI} [A-Z]
With:
RewriteCond %{REQUEST_URI} [A-Z0-9\-]
And see if it helps.
Cheers