Added middleware and template rewrite