Refactor and add new rewrite rules

Signed-off-by: José Valim <jose.valim@plataformatec.com.br>