Is there some duplicate code or something?  Seems like an awful lot of case checks above.  If all your URLs and redirects are working right, that should not be required.  Should only need one.