Fix for domains that end in dots and simplified logic for rewrite domains.
Merge branch 'production' into staging