move rack-rewrite checks to application.rb and insert into top of middleware stack