Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 6 down vote accepted

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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