improve self-url discovery

(use ports if not standard, use other vars for scheme, move / around as needed)