Skip to main content
added 1 character in body
Source Link
JDB
  • 680
  • 6
  • 21
global with sharing class Banzai_UrlRewriterWebsite_UrlRewriter implements Site.UrlRewriter {
    Map<String,String> FRIENDLY_URLS = new Map<String,String>{
        '/pretty_resource_name' => '/resource/Website_ResourceName'
    };

    global PageReference[] generateUrlFor(PageReference[] yourSalesforceUrls){ 
        return null;
    }
    
    global PageReference mapRequestUrl(PageReference friendlyRef) {
        System.debug('**** friendlyRef: ' + friendlyRef);
    
        // Get the url for our request 
        String url = friendlyRef.getUrl().toLowerCase();

        if ( FRIENDLY_URLS.containsKey(url) ){
            PageReference ref = new PageReference(FRIENDLY_URLS.get(url));
            System.debug('**** uglyRef: ' + ref);
            System.debug('**** content: ' + ref.getContent());
            return ref;
        }

        return null;
    }
}
global with sharing class Banzai_UrlRewriter implements Site.UrlRewriter {
    Map<String,String> FRIENDLY_URLS = new Map<String,String>{
        '/pretty_resource_name' => '/resource/Website_ResourceName'
    };

    global PageReference[] generateUrlFor(PageReference[] yourSalesforceUrls){ 
        return null;
    }
    
    global PageReference mapRequestUrl(PageReference friendlyRef) {
        System.debug('**** friendlyRef: ' + friendlyRef);
    
        // Get the url for our request 
        String url = friendlyRef.getUrl().toLowerCase();

        if ( FRIENDLY_URLS.containsKey(url) ){
            PageReference ref = new PageReference(FRIENDLY_URLS.get(url));
            System.debug('**** uglyRef: ' + ref);
            System.debug('**** content: ' + ref.getContent());
            return ref;
        }

        return null;
    }
}
global with sharing class Website_UrlRewriter implements Site.UrlRewriter {
    Map<String,String> FRIENDLY_URLS = new Map<String,String>{
        '/pretty_resource_name' => '/resource/Website_ResourceName'
    };

    global PageReference[] generateUrlFor(PageReference[] yourSalesforceUrls){ 
        return null;
    }
    
    global PageReference mapRequestUrl(PageReference friendlyRef) {
        System.debug('**** friendlyRef: ' + friendlyRef);
    
        // Get the url for our request 
        String url = friendlyRef.getUrl().toLowerCase();

        if ( FRIENDLY_URLS.containsKey(url) ){
            PageReference ref = new PageReference(FRIENDLY_URLS.get(url));
            System.debug('**** uglyRef: ' + ref);
            System.debug('**** content: ' + ref.getContent());
            return ref;
        }

        return null;
    }
}
Source Link
JDB
  • 680
  • 6
  • 21

Re-routing to Static Resources with Site's UrlRewriter resulting in 404

Our public website has some links to downloadable PDFs. We'd like to use "pretty" URLs, but for some reason we're getting a 404 error when using the Site's UrlRewriter.

The resource itself is available at something like:

/resource/Website_ResourceName

When accessed at this URL, the resource downloads as expected.

In the UrlRewriter, I added a rule that checks for the pretty URL and then returns a PageReference to the actual address above:

global with sharing class Banzai_UrlRewriter implements Site.UrlRewriter {
    Map<String,String> FRIENDLY_URLS = new Map<String,String>{
        '/pretty_resource_name' => '/resource/Website_ResourceName'
    };

    global PageReference[] generateUrlFor(PageReference[] yourSalesforceUrls){ 
        return null;
    }
    
    global PageReference mapRequestUrl(PageReference friendlyRef) {
        System.debug('**** friendlyRef: ' + friendlyRef);
    
        // Get the url for our request 
        String url = friendlyRef.getUrl().toLowerCase();

        if ( FRIENDLY_URLS.containsKey(url) ){
            PageReference ref = new PageReference(FRIENDLY_URLS.get(url));
            System.debug('**** uglyRef: ' + ref);
            System.debug('**** content: ' + ref.getContent());
            return ref;
        }

        return null;
    }
}

When I try to access the resource via the pretty URL, I get the following log:

21:01:23.017 (17136662)|CODE_UNIT_STARTED|[EXTERNAL]|core.dns.filter.SiteUrlMapper
21:01:23.024 (24106109)|SYSTEM_MODE_ENTER|false
21:01:23.024 (24160299)|SYSTEM_MODE_EXIT|false
21:01:23.024 (24242464)|SYSTEM_MODE_ENTER|false
21:01:23.024 (24305533)|SYSTEM_MODE_EXIT|false
21:01:23.024 (24325227)|SYSTEM_MODE_ENTER|false
21:01:23.025 (25373838)|SYSTEM_MODE_EXIT|false
21:01:23.026 (26023423)|SYSTEM_MODE_ENTER|false
21:01:23.026 (26144566)|USER_DEBUG|[83]|DEBUG|**** friendlyRef: System.PageReference[/pretty_resource_name]
21:01:23.026 (26437293)|USER_DEBUG|[114]|DEBUG|**** uglyRef: System.PageReference[/resource/Website_ResourceName]
21:01:23.153 (153071380)|USER_DEBUG|[115]|DEBUG|**** content: Blob[306508]
21:01:23.153 (153111859)|SYSTEM_MODE_EXIT|false
21:01:23.153 (153199937)|CODE_UNIT_FINISHED|core.dns.filter.SiteUrlMapper
21:01:23.155 (155600516)|EXECUTION_FINISHED

Note that the pretty URL was correctly translated into the ugly URL, and that the getContent() call on the PageRefernce returned data (meaning it's pointing to something real). But on the page itself, I end up with a 404 error:

Page Not Found: pretty_resource_name 

Stay tuned. Please email us if you need to get in touch.

I can't find any documentation that says what I'm trying to do is not supported. I've verified that the static resource is using public cache control and is available when accessed via the "ugly" URL.