Debug your Apache mod_rewrite Rules!

Apache mod_rewrite is incredibly powerful but it’s also a PITA if you’re trying to do something advanced. Luckily, you can log mod_rewrite’s activities and better understand why your rules aren’t working.

Just add RewriteLogLevel and RewriteLog to your Apache VirtualHost config:

 <VirtualHost *:80>
 # ...

RewriteLogLevel 8
 RewriteLog /var/log/apache2/rewrite.log

# ...
 </VirtualHost>

I suggest log level 8 but you can try lower. Logging rewrites will be SLOW: do not enable in production.

Sample rewrite log output:

 [perdir /var/www/local.dev/www/] RewriteCond: input='/small/asdfsadf/avatar.jpg' pattern='^(.*)/(.*)/(.*)' => matched
 [perdir /var/www/local.dev/www/] RewriteCond: input='/var/www/local.dev/www/small/avatar.jpg' pattern='-f' => matched
 [perdir /var/www/local.dev/www/] rewrite 'small/asdfsadf/avatar.jpg' -> '/small/avatar.jpg'
 [perdir /var/www/local.dev/www/] internal redirect with /small/avatar.jpg [INTERNAL REDIRECT]

Details: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritelog

Pretty print to console in Node.js

JSON.stringify is pretty cool, in fact you can use it to print javascript data structures in a more readable format.

Of course modern browser javascript consoles already format nested data for you, but this is really helpful when using console.log() in Node.js

console.log(JSON.stringify(window.location, null, 2));

// prints...
{
  "ancestorOrigins": {
    "length": 0
  },
  "origin": "http://unsharptech.com",
  "hash": "",
  "search": "",
  "pathname": "/category/web-dev/",
  "port": "",
  "hostname": "unsharptech.com",
  "host": "unsharptech.com",
  "protocol": "http:",
  "href": "http://unsharptech.com/category/web-dev/"
}

JSON.stringify documentation

Analyzing mysql slow query log

I had yet to have a need for a tool to analyze a MySQL slow query log, after some searching I came across a tool I already had!

Most MySQL installs come with the utility mysqldumpslow which is great for parsing your slow query log. I needed to sort by lock time so I just ran the following:

[code]mysqldumpslow -s l mysql-slow.log | less[/code]

I'm in love with git

A month ago I switched to git from subversion when I started at RealSelf. Now I’m in love.

Why I liked git before:

  • Distributed
  • Fast
  • Github
  • (hip)

Why I love git now:

  • Branching does not disrupt workflow

[code]

# …working on myBranch…

git stash

git checkout someOtherBranch

# …make your changes to someOtherBranch…

git commit

git checkout myBranch

git stash pop

# and I’m right back where I left off!

[/code]

  • Rebase

[code]

# instead of:

git merge master

# and getting a messy commit log.. do this:

git rebase master

# and your commits starting after you branched
# are rewound and applied on top of the latest from master

[/code]

  • The command line interface is so powerful and usable that I don’t rely on GUI tools (except for resolving conflicts)
  • Pull requests (Github again)

Disable Java plugins to avoid drive by installs

As a computer repair technician I clean up a lot of computers with Java based malware. Java is known for its zero-day vulnerabilities and as a popular vector for cross-platform attacks. Not to mention the fact that Java Runtime Environment (JRE) updates never remove old versions, and Java installs and enables plugins for all popular browsers (Chrome, Firefox, and IE). Continue reading “Disable Java plugins to avoid drive by installs”

A good reason to dump OpenDNS

namebench DNS Benchmark Results

Like me, you’ve probably read all sorts of great things about the public DNS service OpenDNS, but one thing you should find out for yourself is how it will impact the speed of your web browsing.

After using OpenDNS name servers for months at work I started to notice that page load times at home (on a much slower connection) were considerably more “snappy” with my ISP provided DNS servers. A quick Google brought up the comprehensive Open Source namebench DNS benchmark tool.

A run of namebench resulted in a pretty html file with numbers and graphs that confirmed my suspicions. My ISP’s (Comcast) DNS servers were 174.4% faster than OpenDNS on average, with Google’s Public DNS coming in a close second place.

It should be noted that namebench is rather smart and it rifles through your browser history in order to compile a personalized set of domains for its testing, so my results are potentially unique – and this is just one test (actually I did three, I also used Alexa top domains for a source to avoid DNS caching – and the results were all similar), at one location. Although once I switched to my ISP’s DNS servers I experienced a noticeable difference in page load times.  Not to mention that my results may change in the future due to server load, poor caching, etc. But with OpenDNS, there was always a slight wait before a page would even start to load, and now that wait is gone. And that’s on a 36Mbps downstream connection.

My points are simple:

  1. Switching to OpenDNS (or any other DNS service) may be a bad thing.
  2. Your DNS server does matter.
  3. namebench is awesome.

It may be a matter of milliseconds but it all adds up, many large websites require dozens of DNS queries for ads, images, scripts and more. Do your own testing before believing the hype.

Regardless, OpenDNS is still a great service: if I ever have the need for a basic content filtering system I will turn to OpenDNS.

Download namebench (for Windows, Mac, or Linux/Unix) and see for yourself how your current DNS stacks up.

Fix a malfunctioning BlackBerry Trackball [video]

A friend of mine had a couple Bolds with trackball issues, we went as far as taking one of them apart and attempting to clean the trackball mechanism with no luck. I finally came across this method on a forum and we were amazed by the results – so amazed that we recorded the process on the second Bold with an HD Flip.