Fix #1401 by integrating @derigel23's change to rewrite host.

Thanks again to @derigel23 for the fix content.

Fixes #1401