protected function detectSubsiteId(HTTPRequest $request)
    {
        if ($request->getVar('SubsiteID') !== null) {
            return (int) $request->getVar('SubsiteID');
        }

        if (SubsiteState::singleton()->getUseSessions() && $request->getSession()->get('SubsiteID') !== null) {
            return (int) $request->getSession()->get('SubsiteID');
        }

        $subsiteIdFromDomain = Subsite::getSubsiteIDForDomain();
        if ($subsiteIdFromDomain !== null) {
            return (int) $subsiteIdFromDomain;
        }

        // Default fallback
        return 0;
    }