Fix permalink bug in uglyurls mode (refs #187)

Tests are included.