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.

Advertisements

Installing VMware Player 2.5.1 in Ubuntu 8 via rpm

I have been running VMware player 2.0.4 since August to launch a windows XP Virtual Machine.  I don’t really use the VM all that often, but when I am doing web development, I need to be able to test in IE, and thats the only way I can do it w/o rebooting to Vista *shudder*.     Towards the end of September it started telling me there was an upgrade available.  I went to the VMware website only to discover they had stopped providing the .tar.gz version of VMware player.  My only options were .bundle or .rpm.    Not really feeling like I wanted to be all that adventurous I ignored the upgrade requests.    Finally after installing the latest kernel patches last night I decided to go ahead and upgrade VMware Player as well since I would have to re-compile the kernel modules again anyway for the new linux kernel.  Having never tried to install and RPM on Ubuntu I was at a loss.

Continue reading

Foxmarks in the Library

Recently I was asked by one of our librarians to come up with a way to easily add bookmarks to our Internet workstations.   A little background on this situation follows.  Our Internet workstations are all Linux based, with a custom version of FireFox.  This custom version of FireFox has a lot of things disabled to keep the browser in a stable stat.   I have removed the ability to modify the bookmarks on the workstation in any way. The event code for drag and drop, the menu items, you name it, its gone.   This has made it impossible for the librarians to manage the bookmarks on the workstations themselves.   Any bookmark changes have required me to connect to the machines via SSH and manually update the bookmarks.html file.    This method was just not convenient, and did not allow for easy updates by any means.

Earlier this week, thanks to the offloading of some tasks to our new employee, I was able to put some thought into this problem.   I needed a solution that was easy to maintain, simple for the librarians to use, and most of all, easy to implement.   That last criteria ruled out creating something from scratch, which I really didn’t want to do anyway.   Earlier in the week a co-worker had mentioned how he had just setup foxmarks on his machine and how it was going to sync his bookmarks both at home and work.   It hit me then, why not take a look at foxmarks.  I downloaded and installed it in my browser, and it did exactly as it was designed to.    I realized then that it was a purely native chrome based app.   This fact about its design made it a perfect candidate for what I needed.

I set to creating a generic account for the library, a simple task thanks to the easy to use interface.  Once that was done, I installed foxmarks into the browser on one of the Internet stations.  Due to the customizations, this was not as easy as it normally would be. ( I have all installation menus, and handlers disabled.  Ooops…) I finally worked out a way to install the extension, and once I did I set it to use the newly created account, and checked the box to save the sync password.   Foxmarks  performed its initial sync, and I was able to confirm the success by browsing the foxmarks website.   I then closed the browser, clicked yes on the dialog to clear all stored personal information and felt confident I had a good start.   I re launched the browser, hit sync, and foxmarks asked me for the password for the sync account.  What? I was certain I checked to save the password.  I checked it again, and restated the browser, again clearing the personal information.  Sure enough, foxmarks asked for the sync password again.   It then occured to me that clearing the personal information was probably clearing the foxmarks password.   That would be a problem for sure.  Confident that the password hurdle could be overcome, I set about to modifying the chrome files, and was able to hid the configuration menu items, and remove the keystroke sequence for accessing the configuration menu.

foxmarks-overlay.xul
    <menupopup id="menu_ToolsPopup">
        <menu id="foxmarks-menuitem" label="Foxmarks" class="menu-iconic"
            image="chrome://foxmarks/skin/images/foxmarks_bug.png"
            accesskey="&menu.accesskey.foxmarks;"
            insertafter="devToolsSeparator" hidden="true">

After the UI modifications were complete I  again launched the browser, and checked over the browser to make sure nothing was visible that could allow malicious patrons to manipulate the setup.    Everything I could think of was tested and it passed with flying colors.  I then added a bookmark via the website, and triggered the sync process.   After entering the password, foxmarks synced and the new item appeared.  Again, I still had to fix the password problem.   I  closed the browser again, this time un-checking the options to clear the password store.  After re-launching foxmarks synced perfectly, remembering its password.  This then confirmed for me that foxmarks was indeed using the firefox password store.  It makes perfect sense that it would, but of course in this situation, it’s very inconvenient.    I again dove into the code that comprises foxmarks.  I found, in the foxmarks-settings.js file two functions that handled the username and password.    As I suspected they ultimately called “return” with the value for the username and password.   A simple one line change to each of these functions resulted in a hard coded username and password that would stay put after a clearing of the password store.  By placing a return() just inside the function declaration we essentially bypass the default actions of  the functions.

 get username() {
	return "our_foxmarks_user_account";
        return this.getCharPref("username", "");
    },

 get passwordNoPrompt() {
        return "our_foxmarks_account_password";
        if (!this.rememberPassword && this.sessionPassword) {

After saving the modified file, I started up firefox and hit the sync keystroke, presto, no request for password or user name, and the browser synced.  I restarted again, making sure to clear the password cache, and again foxmarks worked perfectly.   At this point I deployed the modified version of foxmarks to all of our workstations, and sat back and watched the fun.   Within a short period of time, every workstation had correctly synced its bookmarks and all was well.

Finally I  sat down with the librarians and explained how to work with foxmarks’ website interface.  By the end of the first evening after foxmarks was installed we had a healthy collection of frequently used websites all bookmarked, and categorized.   The sync process has been working well and we have had no real problems.  (there was one point where I had accidentally left one of the browsers restored to its default state and some things were modified, but I caught it quickly and fixed the problem.

This alone would have been a fantastic solution, but not being one to rest on my laurels, today I used foxmarks ability to create RSS feeds from links in folders, and used that data to allow the library to randomly select a link and twitter it.  Yup, more fun with twitter.

The twittered catalog

Tonight, I managed to twitter enable our library catalog.   Now, if you find a book you want to recommend to your friends, just click the “tweet this!” link, and fill in your twitter username and password, and click “tweet”.  As if by magic, the item is tweeted, complete with a link to it in our catalog.    Yup, I’m a geek!   Twitter folks, try it out at:

http://library.waubonsee.edu/catalog/

So, what’s that wierd box for John?

A few months back I posted a picture of a strange looking box. I also asked people to guess what the box did. There were some good guess, but none that hit it 100%.

The box, contained a programmable micro controller board. It also contained a small circuit to interface with the “Try Me” button for an off the shelf Gargoyle I bought at Walgreens.

The box, combined with the gargoyle, some home brew LED spotlights, a hacked Dance Dance Revolution Mat, a plaster pedestal I painted to look like marble, and a nice alcove in a set of our bushes, creates my latest scene for Halloween.

The visitor walks past a set of bushes, when suddenly the alcove lights up, showing a gargoyle, the gargoyle opens his eyes, and begins to speak. When he is finished, he closes his eyes again, and the lights dim.

Just imagine the fun this will cause.

RF Remote Prop-1 interface

Today, I finished creating the Prop-1 to RC controller interface circuit. The schematic, for ExpressSCH is available here.

Interface Schematic
Interface Schematic

It works like a charm, all simple transistor logic, no relays, no nothing. Now, with a simple 3 lines of code in the Prop-1 program I can remotely trigger a prop ~30 feet away.


HIGH Bkwd
PAUSE 300
LOW Bkwd

Completed circuit
Completed circuit

One cool thing of note, I submitted the schematic to the support forum for the Prop-1. The head programmer / developer gave it his ok, and complimented me on part of the design.

More on the remote front

Well, I couldn’t stop at just the crow, and the flying ghoul. I found in my bins of stuff a screaming head I got from Target a few years ago. I never did set it out anywhere, but I took a look at it, and found it was indeed quite hackable. I dropped in the receiver board from another of the remote control cars. This time though, I had already used the Forward,and Reverse signals for other props, so had to do a little hacking to the receiver. Fortunately the chip in the car is capable of five separate actions, and the chipset is documented on the net, so I used one of the unused channels and linked it into the head. I then had to build a remote that had buttons for all five signals. I built that, finished the interfacing of the receiver and now have the ability to trigger the head from a remote. Tomorrow morning I’ll build a second transmitter for the Prop-1 to use to transmit.