Simple musings of a Drupal developer.

using RewriteMap

Email |
|
By krs · July 30, 2010 · 0 Comments · 213 Views

This is an Apache Directive that I've never had to use before, but it came in very handy for a very specific problem.

There was already an apache redirect (RewriteRule + RewriteCond) in place, but the destination URL was case sensitive! That's not normally a problem, but it was for an ad server, and the variables were coming in as uppercase, but needed to be lowercase after the redirect. Bad programming on the part of the ad server in my opinion, but we're not going to let that stop us! :)

RewriteMap to the rescue!

First off, the actual directive is a lot like a function definition, and it can only go in a config file or vhost, it's not allowed in a .htaccess file. Luckily the one we want to use is built in, so we just make it available with:

RewriteMap lc int:tolower

This makes the "lc" function is available in our rewrite rules. We start off with the condition and basic rule ...

# Send ads to new ad server
RewriteCond %{HTTP_HOST} ^old\.ad\.server\.com$ [NC]
RewriteRule ^.*site=([^/]+)/.*area=([^/]+)/.*$ http://newad.sever.net/ad/$1/$2 [R,L,NC]

To use the RewriteMap function in your RewriteRule just change the $N replacement to ${lc:$N}, and you're all set. So for example $1 becomes ${lc:$1}. In our example above, the new RewriteRule looks like this:

RewriteRule ^.*site=([^/]+)/.*area=([^/]+)/.*$ http://newad.server.net/ad/${lc:$1}/${lc:$2} [R,L,NC]
Filed in:
Tagged with: , ,

Post New Comment

If you are already an OnSugar member, or would like to receive email alerts as new comments are made, please login or register for OnSugar.
The content of this field is kept private and will not be shown publicly.

About Me

Drupal.org username: kscheirer

subscribe

Drupal Kiva Team

Archive

Archives

July 2010
SMTWTFS
 123
45678910
11121314151617
18192021222324
25262728293031

Drupal Cred

I am going to Drupal Camp Los Angeles 2010 this August 7-8th