only replace trailing slash if necessary