Is the value of the "file_default_scheme" set at this point?    This condiction is used in different places.  Do you think you can use if (vget ('intranet_site', 0))  ?