Nested ReWrite Maps

Today, in helping out a co-worker I came up with a pretty cool ModRewrite rule set. The rule is to be used to migrate users web pages to a new server with new user names. The rewrite will map the old user names to the new user name automatically, allowing existing sets of web pages to easily transition to the new user name without any old links breaking. This was accomplished using two RewriteMaps. The first map is used to convert any case of the old user name to lowercase. This is handled using the int:tolower function.
RewriteMap upper2lower int:tolower

The second RewriteMap is used to perform the actual user lookup. This is accomplished by creating a map file that contains a space separated pair of the form: lowercase_old_user_name new_user_name

oldusernameone Z00000001
oldusernametwo Z00000002
...

The map file is then put into play by using the line:

RewriteMap usermap txt:/etc/apache2/userlist.txt

There were two prospects to the overall rewrite that posed a bit of a challenge to me. The first challenge was to prevent the rewrite from occurring when the URL request contained the new format of user name. This was ultimately accomplished using the RewriteCond statement.

RewriteCond %{THE_REQUEST} !/~[Zz][0-9]{8}

The new user names takes the format of a single, specific, alpha character followed by 8 numeric digits. This made for an easy regular expression to match on, and using the ! character negates the normal “does match” condition of the regular expression into a “does not match” condition.

After that was solved the next problem to solve turned out to be the most difficult of all. I could not find any instructions on the net about how to properly nest two RewriteMap maps inside of each other. My first solution was slightly creative, in that I used two chained ReWrite rules. The first rule assigned the lower cased user name to an apache environment variable, then the second rule used that variable as the input to the second map. While this solution did work, it seemed really convoluted, and I knew there had to be a simpler way to do it. After walking away from the rule for a little bit, I came back to it and hit upon the correct method to nest the two maps.

${usermap:${upper2lower:$1}}

Translated into English this essentially reads as, take the first match and pass it to the upper2lower RewriteMap, and take the results of that RewriteMap and pass it into the usermap RewriteMap. Or more simply, make the user name from the URL lower case, and then lookup the lower case value in the file userlist.txt.

Next up, I just had to iron out the regular expression needed to match and replace the old user name and still retain the remaining parts of the URL, while preventing user names that do not match in our lookup to still trigger a standard 404 error.

^/~([A-Za-z0-9]+)/?(.*)

Pretty straight forward over all. In English it essentially reads:
Match the beginning of the line with /~. Take the next one or more (+) A-Z, a-z, or 0-9 characters and place them into variable $1. Place any characters following an optional / into $2. There is a flaw in this, but it should not be an issue in our implementation. If you can identify it, or better yet fix it, please post your solution in the comments. 🙂

One additional touch, that I felt was important to add was the R=301 flag. This causes apache to generate an HTTP 301 permanent redirect header. This should help Google and the like properly index the new URLs. The final rewrite code block follows.

RewriteEngine On
RewriteMap upper2lower int:tolower
RewriteMap usermap txt:/etc/apache2/userlist.txt
RewriteCond %{THE_REQUEST} !/~[Zz][0-9]{8}
RewriteRule ^/~([A-Za-z0-9]+)/?(.*) http://www.example.com/~ ${usermap:${upper2lower:$1}}/$2 [L,R=301]

I’d love to hear comments, or improvements on the rule set.

Advertisement

Illinois Government

Illinois government has a long way to go yet. I’m happy that Pat Quinn is in the office now, and I hope he can indeed pull off his desire to clean up the government. He’s going to have a rough road ahead of him, but for all of our sakes, I hope he sticks to his guns and really does fumigate the state government.
I was shocked to hear that Rickey Hendon was brazen enough to be caught on CBS news saying, “The biggest challenge that I see for Pat Quinn is to overcome the do-gooder, reformer image. Pat has to know that you have to grease the wheels — that’s the way it works — to get things done around here.”
Holy carp… Has this man not been paying attention to what just happened to Blago?!?!?
Governor Quinn, I think you found your first cockroach to exterminate. As a citizen of Illinois, I do NOT want him to overcome the do-gooder image, keep it up!!! Reform away mon’ capitan! We need it!