2

we have a page that can be accessed both via regular HTTP and HTTPS. We now stumbled across an issue that if a page has been loaded via HTTP (and the content has thus been cached) and another user tries to load the same page via HTTPS afterwards, the browser complains about loading non-secure content, as Craft serves the cached content (e.g. URLs to images) that was generated while accessing the page via HTTP.

Shouldn't the {% cache %} tag take the protocol into account when comparing URLs? Is there a known workaround or best practice for handling this?

Thanks,

Clemens

2 Answers 2

2

I've never done this, but you should be able to use the cache using key parameter with the craft.request.isSecure property to do this.

isSecure

Whether the current request is over SSL.

Something like:

{% cache using key (craft.request.isSecure ? 'https': 'http') %}
2

Something like this would work:

{% if craft.request.isSecure() %}
    {% set cacheKey = 'secure' %}
{% else %}
    {% set cacheKey = 'insecure' %}
{% endif %}

{% cache using key cacheKey %}
    ...
{% endcache %}

See here for more info.

1
  • But isn't it a bit inconvenient and error-prone to manually have to think about this if-else block every time the %cache% tag is used? Maybe the check for secure/insecure request should be incorporated into the default behavior? Commented Feb 16, 2015 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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