Process template redirects correctly

Also strip the template parameters so any whitespace
doesn't get in the way. Minor PEP8 fixes too.

Change-Id: I04206f6ebd901d649ff9c12d49e8e7f37f2f8d6d
Merge "Process template redirects correctly"