added dir check to rewrite script