Forum Moderators: phranque

Message Too Old, No Replies

Mod_Rewrite case conversion

Change any character in the url from uppercase to lowercase.

         

Imaster

12:18 pm on May 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

I want to do a 301 redirect for some urls on my site. The case is to change any character in uppercase to lowercase in the url.

For eg:
Current: http://domain.com/widgets/A.html to
Required: http://domain.com/widgets/a.html

OR

Current: http://domain.com/widgets/M-1.html to
Required: http://domain.com/widgets/m-1.html

The case change will occur only for the html files and not the directories as the directories are all in the lowercase, but some html files are in uppercase.

Can anyone provide me with a simple code that I can use in my .htaccess file?

Perhaps two coding options could work here:

- Change any character in the entire url from uppercase to lowercase.
OR
- change any character in the "*.html" part of the url from uppercase to lowercase. (Only this case applies for me)

Please assist! TIA

[edited by: jdMorgan at 2:08 pm (utc) on May 17, 2004]
[edit reason] Fixed thread title [/edit]

gergoe

12:45 pm on May 17, 2004 (gmt 0)

10+ Year Member



You can use the tolower built-in apache function together with the RewriteMap directive to change any regex match to lowercase. Check out the mod_rewrite documentation [httpd.apache.org] and the URL rewriting guide [httpd.apache.org] about the possible implementations of this.

jdMorgan

2:13 pm on May 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Imaster,

The problem is that RewriteMap is not available in an .htaccess context. If you can put the code into your server's httpd.conf file, then the tolower: function given in the RewriteMap example (see Apache mod_rewrite documentation) will work nicely.

Otherwise, see this thread [webmasterworld.com] for a method that works in .htaccess. But note the warnings about speed degradation for the most general case of converting any number of uppercase letters anywhere in the URL to lowercase, and adapt the code for your needs to avoid this.

Jim

Imaster

3:19 pm on May 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks gergoe and Jim,

I don't quite understand how to do it and my server admin is out of town for a few days. I do play around with the httpd.conf and other configuration files and am aware of the basic tricks.

However if you could provide me with exact steps and code that i need to follow to make this happen, I would be pleased to try it out myself :)

TIA

p.s. - Thanks Jim for fixing the thread title :)

jdMorgan

3:45 pm on May 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Imaster,

Please review our charter [webmasterworld.com]. Hopefully, we can point you in the right direction.

Something like this might do what you want, but you'll have to research it and test it.


RewriteMap lowercase int:tolower
RewriteCond ${lowercase:%{REQUEST_URI}¦NONE} ^(.+)$
RewriteRule .* http:www.yourdomain.com%1 [R=301,L]

or maybe a shorter version:

RewriteMap lowercase int:tolower
RewriteRule .* http:www.yourdomain.com${lowercase:%{REQUEST_URI}} [R=301,L]

See the Apache mod_rewrite [httpd.apache.org] RewriteMap documentation [httpd.apache.org] and the Apache URL Rewriting Guide [httpd.apache.org].

Jim

[edit] changed internal rewrite to requested 301 external redirect [/edit]

Imaster

4:00 pm on May 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks a bunch, Jim. I will check it out.

Imaster

11:35 am on May 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Jim,

I used another method "Module mod_speling". This solved my worries and added more power to what I needed. In case of minor spelling mistakes in the urls, they get correct and the module also takes care of the uppercase to lowercase. :)

jdMorgan

5:08 pm on May 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, mod_speling works well for this in a limited sense -- It corrects one spelling error per URL. For some reason, I thought you needed to convert multiple characters; Otherwise, I would have mentioned it.

Jim