7

I know you can serve static HTML by uploading it as a static resource, but is it possible to create a URL rewrite rule for a static resource?

Something like this

if (url.startsWith('/coach')){    
    return new PageReference('/resources/coach/index.html');
}

Navigating directly to /resources/coach/index.html works, but the rewrite rule doesn't?

Ideally I'd love to do something even more advanced like this, to be able to access any file in a zip'd static resource.

List<String> segments = url.split('/');

if (url.startsWith('/coach')){    
    return new PageReference('/resources/coach/'+ segments[1]);
}

Any ideas?

6

Disclaimer: a 'tricky' attack surface indicates this may not be the best kind of problem to solve with Salesforce ;) so double check your real business requirements and push back on those if necessary!

First idea:

You might use the Site.UrlRewriter interface to do this on a Force.com Site, for eg:

public class UrlRewriter implements Site.UrlRewriter {

    public PageReference mapRequestUrl(PageReference incoming) {
        return new PageReference('/resource/1394899484000/SiteSamples/SiteStyles.css');
    }

    public PageReference[] generateUrlFor(PageReference[] yourSalesforceUrls) {
        return null;
    }
}

However the return signature of the implementor must be a PageReference resolving to a real page! I tried this for real and Salesforce serves a 404, not the file inside a zipped static resource!

Second idea:

You could create a real Visualforce Page as a kind of proxy that serves up the contents of the zip file using Apex, and point your URL Rewriter at that page:

Example: https://stackexchange-developer-edition.na14.force.com/coach/foo/bar

UrlRewriter.cls

public class UrlRewriter implements Site.UrlRewriter {

    public PageReference mapRequestUrl(PageReference incoming) {
        return new PageReference('/apex/ResourceReader');
    }

    public PageReference[] generateUrlFor(PageReference[] yourSalesforceUrls) {
        return null;
    }
}

ResourceReader.page

<apex:page controller="ResourceReaderController" contentType="text/plain">
    <apex:outputText value="{!Content}" escape="false" />
</apex:page>

ResourceReaderController.cls

public class ResourceReaderController {

    public String getContent() {
        return new PageReference('/resource/1394899484000/SiteSamples/SiteStyles.css').getContent().toString();
    }

}

Third idea:

Maybe try using Site URL Redirects which can do a 301 or 302 redirect you. It won't be transparent to the visitors, but you can see an example of that here:

Example: https://stackexchange-developer-edition.na14.force.com/coach/herp/derp

However these guys exist as 0H0 prefix objects and must be administered through the user interface. I'm not sure you will ever be able to upload a zip file and automatically drill into it.

redirect

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.