support redirect by using correct document URI

This is a squashed commit from pull request #98