<p>One method is using something like the following in your <code>general.php</code> file.  Using this method the config settings will take on the scheme of the request being made.  I've set both the <code>siteUrl</code> and example <code>environmentVariables</code> below:</p>

<pre><code>&lt;?php

// Ensure our urls have the right scheme
define('URI_SCHEME',  ( isset($_SERVER['HTTPS'] ) ) ? "https://" : "http://" );

// The site url
define('SITE_URL',    URI_SCHEME . $_SERVER['SERVER_NAME'] . '/');

// The site basepath
define('BASEPATH',    realpath(CRAFT_BASE_PATH . '/../') . '/');

return array(
    '*' =&gt; array(

        'siteUrl' =&gt; SITE_URL,

        'environmentVariables' =&gt; array(
            'siteUrl'  =&gt; SITE_URL,
            'basePath' =&gt; BASEPATH
        ),
    ),
);
</code></pre>

CC BY-SA 3.0
2014-11-14T00:46:50.200Z
2014-11-14T00:46:50.200Z
Ben Parizek
<p>Try setting it as the actual <a href="http://buildwithcraft.com/docs/config-settings#siteUrl" rel="nofollow"><code>siteUrl</code> config setting</a>, rather than a <a href="http://buildwithcraft.com/docs/multi-environment-configs#environment-specific-variables" rel="nofollow"><code>siteUrl</code> environment variable</a>.</p>

<p>See <a href="https://craftcms.stackexchange.com/questions/920/what-s-the-recommended-way-to-set-the-site-url/921#921">here</a> for an explanation.</p>

CC BY-SA 3.0
2014-11-13T23:59:32.020Z
2014-11-15T18:26:24.430Z
2014-11-15T18:26:24.430Z
Brad Bell
<p>I've already answered this question but ran into another situation that I feel merits a separate answer instead of making my first answer more complicated.</p>

<p>In some cases, we use Cloudflare Flexible SSL.  Due to the way Cloudflare does SSL, this adds support for SSL but not via a certificate on the server or via the standard SSL port so the common <code>$_SERVER['HTTPS']</code> variable isn't available to check nor are you able to just check to see if the secure port is in use.</p>

<p>The solution I came across is to check for the <code>$_SERVER['HTTP_X_FORWARDED_PROTO']</code> variable.  This may not work in all situations, but it worked for me and can be substituted in for the URI_SCHEME check in <a href="https://craftcms.stackexchange.com/a/4734/115">the answer above</a>:</p>

<pre><code>if(!empty($_SERVER['HTTP_X_FORWARDED_PROTO']))
{
    // Support Cloudflare SSL
    define('URI_SCHEME', $_SERVER['HTTP_X_FORWARDED_PROTO'].'://');
}
else
{   
    // Standard SSL support
    define('URI_SCHEME',  ( isset($_SERVER['HTTPS'] ) ) ? "https://" : "http://" );
}
</code></pre>

<p>If anyone would like to edit and add any other Cloudflare specific fixes, feel free.  I surely don't have the domain knowledge in this area to claim this is a final answer, just one that helped me out in getting assets serving over SSL on a Cloudflare-enabled site.</p>

CC BY-SA 3.0
2015-02-04T06:57:39.957Z
2015-02-04T06:57:39.957Z
2017-04-13T12:50:59.930Z
Ben Parizek
<p>I'm looking for a way to set up the general.php file's siteUrl to be able to use both http and https. Only the front-end login would be using the https. This is how I currently have it set up:</p>

<pre><code>'mysite.com' =&gt; array(
    'cooldownDuration' =&gt; 0,
    'environmentVariables' =&gt; array(
        'siteUrl' =&gt; '//mysite.com/',
    )
)
</code></pre>

<p>The problem with this is when the front-end form is submitted and the user is redirected, the url messes up (i.e.: mysite.com/mysite.com/page-name)</p>

<p>Any suggestions would be great!</p>

<users><url><config><siteurl><https>
How to set up {siteUrl} to work with both http and https
# How to set up {siteUrl} to work with both http and https

(Asked by: username_0 on 2014-11-13)

I'm looking for a way to set up the general.php file's siteUrl to be able to use both http and https. Only the front-end login would be using the https. This is how I currently have it set up:
'mysite.com' => array(
    'cooldownDuration' => 0,
    'environmentVariables' => array(
        'siteUrl' => '//mysite.com/',
    )
)

The problem with this is when the front-end form is submitted and the user is redirected, the url messes up (i.e.: mysite.com/mysite.com/page-name)
Any suggestions would be great!

**Tags:** users, url, config, siteurl, https


---

## 3 answers

### Answer by username_1 on 2014-11-14. Score: 12

One method is using something like the following in your general.php file.  Using this method the config settings will take on the scheme of the request being made.  I've set both the siteUrl and example environmentVariables below:
 array(

        'siteUrl' => SITE_URL,

        'environmentVariables' => array(
            'siteUrl'  => SITE_URL,
            'basePath' => BASEPATH
        ),
    ),
);


---

### Answer by username_2 on 2014-11-13. Score: 6

Try setting it as the actual [`siteUrl` config setting](http://buildwithcraft.com/docs/config-settings#siteUrl), rather than a [`siteUrl` environment variable](http://buildwithcraft.com/docs/multi-environment-configs#environment-specific-variables).

See [here](https://craftcms.stackexchange.com/questions/920/what-s-the-recommended-way-to-set-the-site-url/921#921) for an explanation.


---

### Answer by username_1 on 2015-02-04. Score: 5

I've already answered this question but ran into another situation that I feel merits a separate answer instead of making my first answer more complicated.
In some cases, we use Cloudflare Flexible SSL.  Due to the way Cloudflare does SSL, this adds support for SSL but not via a certificate on the server or via the standard SSL port so the common $_SERVER['HTTPS'] variable isn't available to check nor are you able to just check to see if the secure port is in use.
The solution I came across is to check for the $_SERVER['HTTP_X_FORWARDED_PROTO'] variable.  This may not work in all situations, but it worked for me and can be substituted in for the URI_SCHEME check in the answer above:
if(!empty($_SERVER['HTTP_X_FORWARDED_PROTO']))
{
    // Support Cloudflare SSL
    define('URI_SCHEME', $_SERVER['HTTP_X_FORWARDED_PROTO'].'://');
}
else
{   
    // Standard SSL support
    define('URI_SCHEME',  ( isset($_SERVER['HTTPS'] ) ) ? "https://" : "http://" );
}

If anyone would like to edit and add any other Cloudflare specific fixes, feel free.  I surely don't have the domain knowledge in this area to claim this is a final answer, just one that helped me out in getting assets serving over SSL on a Cloudflare-enabled site.


---

