Archive for June, 2009

How to avoid looping in mod_rewrite redirection

Tuesday, June 30th, 2009

When redirecting things to a new location that fits the original rewrite pattern, you end up with a recursive feedback loop and all you get is an error. If you specified a URL scheme and a domain in your rewrite (meaning that the redirection is visible to the browser) you’ll see the error in your browser saying something along the lines of “the page tried to redirect too many times”. Otherwise, the redirection will be internal, and you’ll get your server’s 500 Internal Server Error page.

redirect_loop_safari

So, how do you avoid it? Well, say you wanted to redirect some regex (regular expression) pattern to the your index page, say index.php, but it turns out that “index.php” actually matches your pattern, so index.php redirects to index.php which redirects to index.php, and so on, and so forth.

To prevent this, simply add a RewriteCond that checks the REQUEST_URI to make sure it doesn’t match the string that you redirect to. The following is an example.

RewriteCond %{REQUEST_URI} !^/?index.php$
RewriteRule ^(regex|goes|here)$ /index.php

Just make sure the regex in the first part does not match any of the pages you want to redirect, and that’s it! You’re done.

Best of luck on getting this to work the way you want it to, and happy web developing!

Customization of the user interface in Mac OS X

Tuesday, June 16th, 2009
Finder File Menu: Return = Open

Finder File Menu: Return = Open

Sometimes I find myself very frustrated with even the smallest of UI annoyances, and with this little trick I discovered, I just fix them.

Requirements:

1. A computer running Mac OS X

2. A copy of the Apple Developer Tools (XCode, etc.)

3. Some inclination as to what the hell you are doing.

All you really have to do is open an Application’s folder (by ctrl-clicking on it and selecting “Show Package Contents”, navigate to Contents -> Resources,  find the appropriate .nib file, and open it with Interface Builder.app. Once that’s done, edit the file to your needs and save. Isn’t that easy?

As an example, I’ll go through the process of giving the Finder’s ‘Open’ menu item the Return keyboard shortcut. Normally this would be possible just by creating a new entry in System Preferences -> Keyboard & Mouse -> Keyboard Shortcuts, but unfortunately Apple doesn’t let us use a single-key as a shortcut, (although something like cmd-Return would work).

Let’s start by finding the Finder in /System/Library/CoreServices/. Control-click on Finder.app and select Show Package Contents, giving you a new Finder window.

Show Package Contents

Now open Contents then Resources. Since my computer is in English, I opened English.lproj (sometimes called en.lproj in other apps). Then find the item Menus.nib. Although on a normal application we could just open it, since the Finder is located in /System we’ll have to copy it somewhere else, edit it there, and then copy it back. So drag Menus.nib to your desktop, and then open it. (You might want to duplicate the .nib before editing it, just to have a backup somewhere.)

Click the File menu item you’ll see (in the Tiger menu bar style), and select ‘Open’. Bring up the Attributes Inspector (cmd-1 or through the Window menu). From there you can edit or clear the keyboard equivalent, just select the box and press Return!

Interface Builder Inspector window

Now save it, drag the Menus.nib file from your Desktop back into the English.lproj folder, authenticate with an administrator username and password, and Relaunch the Finder. Congratulations, you now have your own custom Finder.app.

Feel free to play around with this trick as much as you like, and let me know if you do anything cool with it. I have been for a while, and nothing terrible has happened to me yet. That having been said, I take no responsibility for any unknown dark powers you may evoke whilst exploring the unfathomable inner depths of your computer. Watch out for Balrogs.

Getting rid of “ACL found but not expected” errors when repairing permissions

Monday, June 15th, 2009

I’ve seen quite a lot of people complaining about these errors, and even though they don’t really hurt anything they can be very annoying.

So, in my very expansive free time I devised and wrote a program that will take the error messages from repairing (or verifying) permissions and remove all ACL from the files that are reported.

It is written in Python, and runs through from command line. As you shouldn’t trust any random person’s code to just run on your computer, I recommend opening the script in your favourite text editor before letting it steal all your passwords, just to be safe.

Disclaimer: I take no responsibility for anything the program does, use it at your own risk. It’s very likely to reduce your computer to a smoldering pile of melted plastic and spare parts.

Download removeUnexpectedACL.zip (outdated, see below)

All kidding aside, it’s pretty harmless, and damn are those ACL errors ever annoying.

Update: I changed the name. Download it here instead.

Update #2: removeUnexpectedACL has been renamed “ACLr8″ and now has its own page here.

A subdomain for each directory with .htaccess and some mod_rewrite wizardry

Wednesday, June 10th, 2009

I don’t know how useful this will be to anyone, but I was playing around with my webserver a while back and because subdomains are pretty cool I thought I would figure out for myself how to do this.

You may use either httpd.conf or an .htaccess at your document root.

Make sure you have mod_rewrite turned on before you begin.
RewriteEngine On

First, use a RewriteCond like this to prevent redirection things like your favicon.ico that need to be available everywhere. Change this based on your setup.
RewriteCond %{REQUEST_URI} !^/(favicon.ico|images/.+|javascript/.*)$

Then, capture the subdomain in %1.
RewriteCond %{HTTP_HOST} ^(.+)\..+\..+$
This next line just prevents looping by making sure the REQUEST_URI doesn’t match the what’s in %1. Don’t ask how it works.
RewriteCond %1,%{REQUEST_URI} !(^[^,]+),/\1.*

This checks to see if what we have in %1 is in fact a directory under the document root.
RewriteCond /your/document/root/%1 -d

And finally, redirect ‘/anything’ to ‘/subdomain/anything’.
RewriteRule ^(.*)$ /%1/$1 [L]

And there we go, that should do it. All together, that’s:
RewriteCond %{REQUEST_URI} !^/(favicon.ico|images/.+|javascript/.*)$
RewriteCond %{HTTP_HOST} ^(.+)\..+\..+$
RewriteCond %1,%{REQUEST_URI} !(^[^,]+),/\1.*
RewriteCond /your/document/root/%1 -d
RewriteRule ^(.*)$ /%1/$1 [L]

Questions, comments, criticism, and other feedback are very welcome. If you have a better way of doing it, let me know!

Hello, world!

Tuesday, June 9th, 2009

First.