John’s Musings

1/30/2009

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.

For some reason I REALLY want …

Filed under: On the Road — Tags: — John @ 6:27 pm

For some reason I REALLY want to try this! http://www.xkcd.com/537/ Hmmmmm I wonder if it would work at the Dirty Duck? ;-) :-)

Illinois Government

Filed under: Political, The Duh! File — Tags: , , , , — John @ 6:15 pm

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!

Ahhh feels good to not be in a…

Filed under: On the Road — John @ 5:18 pm

Ahhh feels good to not be in a panic over the portal.

about to do load testing

Filed under: On the Road — John @ 9:02 am

about to do load testing

1/29/2009

REJOICE Fellow Illinois reside…

Filed under: On the Road — Tags: — John @ 4:53 pm

REJOICE Fellow Illinois residents! Blago has been removed!!!

Lunch with Anders and Nick in …

Filed under: On the Road — Tags: — John @ 12:46 pm

Lunch with Anders and Nick in the student center.

Talkin’ web server stuff with …

Filed under: On the Road — Tags: — John @ 11:45 am

Talkin’ web server stuff with anders and nick

I woke up with “peter and the …

Filed under: On the Road — Tags: — John @ 6:37 am

I woke up with “peter and the wolf” playing in my head. That’s the 2nd time this month. I wonder what’s triggering it.

1/28/2009

DDR and guitar hero set up in …

Filed under: On the Road — Tags: — John @ 12:31 pm

DDR and guitar hero set up in the student center. Interesting…

1/27/2009

Open mic night, but only Jon p…

Filed under: On the Road — Tags: — John @ 9:41 pm

Open mic night, but only Jon playing again. Come on yorkville , where’s your muscians!

300 user load test had a glitc…

Filed under: On the Road — Tags: — John @ 6:49 pm

300 user load test had a glitch or two but they are easy fixes. Will try again tomorrow.

load test of 100 users went p…

Filed under: On the Road — Tags: — John @ 10:44 am

load test of 100 users went pretty well. Web2 catalog ran out of users, but that was expected… he was starting a new session each request

1/26/2009

Tommor is load test day. I’m n…

Filed under: On the Road — Tags: — John @ 7:31 pm

Tommor is load test day. I’m nervous, but cautiously optomistic.

FINALLY! Everything seems to b…

Filed under: On the Road — Tags: — John @ 2:26 pm

FINALLY! Everything seems to be working (with one minor exception… stupid multi-byte utf-8 character that’s refusing to properly convert)

1/25/2009

I think I have it… I’ll star…

Filed under: On the Road — Tags: — John @ 10:56 pm

I think I have it… I’ll start final testing in the test version of the portal, tomorrow. Now, sleep…

Just completed the initial RES…

Filed under: On the Road — Tags: — John @ 4:04 pm

Just completed the initial REST interface to list our eReference books. Switching to AJAX should increase tab load time by at least 4x

All show stopper bugs apper to…

Filed under: On the Road — Tags: — John @ 1:52 am

All show stopper bugs apper to be cleared up. Leaves tomorrow to attempt fixing one UI display problem and possibly optimize eRef channel.

Took a break to see inkheart w…

Filed under: On the Road — Tags: — John @ 12:35 am

Took a break to see inkheart with Karl & Gabi. Was enjoyable. Home again, now time for a little more coding.

1/24/2009

I’ve wasted 8,550 Seconds or 1…

Filed under: On the Road — John @ 4:06 pm

I’ve wasted 8,550 Seconds or 143 Minutes or 2.38 Hours or 0.1 Days with 285 Tweets on Twitter! http://tweetwasters.com

Older Posts »

Powered by WordPress