<p>I've never done this, but you should be able to use the cache <a href="http://buildwithcraft.com/docs/templating/cache#using-key" rel="nofollow">using key</a> parameter with the <a href="http://buildwithcraft.com/docs/templating/craft.request#isSecure" rel="nofollow">craft.request.isSecure</a> property to do this.</p>

<blockquote>
  <p>isSecure</p>
  
  <p>Whether the current request is over SSL.</p>
</blockquote>

<p>Something like:</p>

<pre><code>{% cache using key (craft.request.isSecure ? 'https': 'http') %}
</code></pre>

CC BY-SA 3.0
2015-02-16T18:13:50.427Z
2015-02-16T18:13:50.427Z
Marion Newlevant
<p>Something like this would work:</p>

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

{% cache using key cacheKey %}
    ...
{% endcache %}
</code></pre>

<p>See <a href="http://buildwithcraft.com/docs/templating/cache#using-key" rel="nofollow">here</a> for more info.</p>

CC BY-SA 3.0
2015-02-16T20:05:00.977Z
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?
Clemens Sum
CC BY-SA 3.0
2015-02-16T18:12:45.983Z
2015-02-16T18:12:45.983Z
Brad Bell
<p>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.</p>

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

<p>Thanks,</p>

<p>Clemens</p>

<cache><https>
Cache HTTP and HTTPS seperately using {% cache %}
# Cache HTTP and HTTPS seperately using {% cache %}

(Asked by: username_0 on 2015-02-16)

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

**Tags:** cache, https


---

## 2 answers

### Answer by username_1 on 2015-02-16. Score: 2 | Accepted answer

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') %}


---

### Answer by username_2 on 2015-02-16. Score: 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.

> **Comments:**
> **username_0**: 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?  

---

