11

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!

3 Answers 3

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:

<?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(
    '*' => array(

        'siteUrl' => SITE_URL,

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

Try setting it as the actual siteUrl config setting, rather than a siteUrl environment variable.

See here for an explanation.

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.

Your Answer

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

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