Question

I have the following Apache Rewrite rule

<IfModule rewrite_module>
     RewriteEngine on
     RewriteMap tolowercase int:tolower
     RewriteCond $2 [A-Z] 
     RewriteRule ^(.*)/(.*).html$ $1/${tolowercase:$2}.html [R=301,L]
</IfModule>

that changes this:

http://localhost.localdomain.com/FooBarBaz.html

to this:

http://localhost.localdomain.com/foobarbaz.html

I'd like to port it to this tuckey.org URL Rewrite Filter.

What is an equivalent rule that I could use to make the URL lowercase? I'm particularly interested in how to form the condition element.

Here's my first cut at the rule, but it doesn't work, even without the condition:

<rule>
    <name>Force URL filenames to lower case</name>
    <from>^(.*)/(.*).html$</from>
    <to type="permanent-redirect" last="true">$1/${lower:$2}.html</to>
</rule>
Was it helpful?

Solution

Here's what I eventually settled on:

<rule match-type="regex">
    <name>Force URL filenames to lower case</name>
    <condition type="request-uri" casesensitive="false" operator="notequal">^.*/a4j.*$</condition>
    <condition type="request-uri" casesensitive="true">^.*/.*[A-Z].*.html$</condition>
    <from>^(.*)/(.*).html$</from>
    <to type="permanent-redirect" last="true">$1/${lower:$2}.html</to>
</rule>

The first condition is to prevent the rule from running on A4J AJAX requests.

OTHER TIPS

Sean,

You don't need a condition to do this (excepting the ignore on the AJAX calls). More importantly, the condition element does not have a casesensitive attribute, only the from element does. Once I realized this, I was able to write the rule as:

<rule match-type="regex">  
    <note>Force URL to lower case</note>
    <from casesensitive="true">^.*[A-Z].*$</from>
    <to type="permanent-redirect" last="true">${lower:$0}</to>  
</rule>

NOTE: This works for the entire path request (though not the querystring).

I stumbled across your post because the URL Rewrite Filter rule I was given that looked a lot like yours was not working. Through a ton of trial and error, I eventually found that the problem wasn't with the regular expression at all. It was that it was matching but was not case sensitive, so I was getting infinite redirects.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top