[Resolved] Cannot find CMSSite associated with current URL

Error "Cannot find CMSSite associated with current URL".
This is because you are not telling Hybris which site you want to access. Let's first see all the possible solutions and then will go into other details.
There are three ways to let Hybris know about the site you are trying to access
1. Pass the CMSSite ID as a request parameter
Simply pass your siteID as a request parameter(?site=SiteID) in your first request which helps the Hybris to understand which site you are trying to access. Let's say I'm trying to access the powertools site then URL would be https://localhost:9002/yacceleratorstorefront?site=powertools
2. Access site using http://<siteID>.local:9001/
Access site with siteID as DNS name. You can make 127.0.0.1 host with <siteID>.local. Let's say I want to access a powertools (It's CMSSite id for powertools), then add an entry like 127.0.0.1 powertools.local in your host file and then access your site usinghttp://powertools.local:9001/yacceleratorstorefront/ instead of localhost
3. Add a new regex to access the site as you want 

Add a new regular expression of your choice in the urlPatterns of your CMSSite. So that you can access your site as you want. Just for an example, let's say, I want to access the site using localhost URL only and without passing ?site=powertools ever. So I need to add a new regex like (?i)^https?://[^/].*$ to urlPatterns of powertools CMSSite. Now I can directly open the powertools site using https://localhost:9002/yacceleratorstorefront/
You can do that using Impex as well
$siteUid=mysite       
# CMS Site                                                                                                 
INSERT_UPDATE CMSSite ; uid[unique=true] ; urlPatterns                                                                                                                  ;      
                      ; $siteUid         ; (?i)^https?://[^/]+(/[^?]*)?\?(.*\&)?(site=$siteUid)(|\&.*)$,(?i)^https?://$siteUid\.[^/]+(|/.*|\?.*)$,(?i)^https?://[^/].*$ ;

If you are interested in the details, keep reading...
For the first request(a new session), Hybris trying to identify which site to serve based on the requested URL. Once it finds the associated CMSSite it keeps that in the session to serve the upcoming request for the same site. To identify the CMSSite, it internally matches the requested URL with CMSSite's urlPatterns attribute. The urlPatterns is a list of regex.
OOTB CMSSite's urlPatterns attribute contains below regex. Currently, I am referring to powertools CMSSite.
(?i)^https?://[^/]+(/[^?]*)?\?(.*\&)?(site=powertools)(|\&.*)$ 
(?i)^https?://powertools\.[^/]+(|/.*|\?.*)$
This means, if you want to access the powertools site then either you pass site=powertolls as request param or your URL should start with http://powertools.https://powertools.
As I mentioned in above 3rd point, you can always add a new regex as you want to access the site. Just make sure to have unique regex for each site If you are having multiple CMSSite.
CMSSiteFilter
    //set current site
    CMSSiteModel cmsSiteModel = getCurrentCmsSite();
    if (cmsSiteModel == null || StringUtils.contains(queryString, "clear=true"))
    {
        final String absoluteURL = StringUtils.removeEnd(currentRequestURL, "/")
                + (StringUtils.isBlank(queryString) ? "" : "?" + queryString);

        cmsSiteModel = getContextInformationLoader().initializeSiteFromRequest(absoluteURL);
    }

    if (cmsSiteModel == null)
    {
        // Failed to lookup CMS site
        httpResponse.sendError(MISSING_CMS_SITE_ERROR_STATUS, MISSING_CMS_SITE_ERROR_MESSAGE);
        return false;
    }
As you can see in the above snippet it throws the error if no final associated CMSSite found. Please note it is always trying to look for the session CMSSite getCurrentCmsSite(), if not found or request has clear=true parameter then only it tries to sort out the associated site for the requested URL. Which means if you want to access another site then either you have to start a new session or pass clear=true along with site parameter, Like ?site=OtherSite&clear=true

Author Image

Ankitkumar Patel

Sr. SAP Hybris consultant, having 15+ years experience in SAP Commerce Cloud (Hybris), SAP Spartacus. Extensive experience in SAP Hybris development, third-party integrations, project architecture and design... Read more

Comments

Popular posts from this blog

Hybris flexible search query examples

How to remove or update all data records in Hybris?

How to Install temporary Hybris license?