Redirect more intelligently to prevent redirect loops

Fixes #218

@danrschlosser @natebrennand