Forum Moderators: phranque

Message Too Old, No Replies

Help with Regex for URL Rewrite

         

Saudiqua

11:50 am on Sep 12, 2011 (gmt 0)

10+ Year Member



Hi There

I want to rewrite all uppercase/mixed case URLs to lowercase using the following rewrite rule:

RewriteRule ^([^A]*)A(.*)$ /$1a$2


Problem is, I will have to do this rewrite rule for each letter of the alphabet...

Is there not a way I can use regular expression to match for uppercase and lower case? for example:

RewriteRule ^([^A-Z]*)[A-Z](.*)$ /$1[a-z]$2


Thanks

Sadie

g1smd

11:55 am on Sep 12, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This is the wrong approach. Using mod_rewrite configured as a redirect is not at all an efficient way to do this.

Instead, use a RewriteRule to detect any URL with one or more uppercase characters and REWRITE (that's rewrite, not redirect) that request to a special PHP script.

In the PHP script, detect the requested URL, use strtolower or similar to lower case the URL, then use two HEADER directives to issue a 301 redirect to the new URL.

The rewrite code should be near the top of your .htaccess file, after the 'deny dodgy requests' section and before any canonicalisation redirects (the PHP script will take also care of canonicalisation for those requests with upper case).

lucy24

4:13 pm on Sep 12, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



To answer the more specific question: no, RegEx does not have a case-changing function :( Some applications that use Regular Expressions have such a function, but that's specific to the application. That's why you have to detour to a php script or equivalent.