Another solution without adding TV to resources.
First, go to system settings (or context setting)
- add force_ssl key (default value = 0)
- add force_ssl_ids, and let the value empty for now
- if your https domain/subdomain is different from site_url, add ssl_site_url key (value = 'www.httpsdomain.com')
Back to above setting, if you want to force HTTPS for all pages, set
force_ssl value to 1, otherwise you can add any pages id to
force_ssl_ids key (comma separated id, ex. 3,30,60).
Second, create a plugin "makeSSL" and attach
OnLoadWebDocument event to it.
<?php
//name: makeSSL plugin
//event: OnLoadWebDocument
$https_port= $modx->getOption('https_port','none',443);
$isSecure= ($_SERVER['SERVER_PORT'] == $https_port || (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS'])=='on')) ? 1 : 0;
if ($isSecure) return;
if ($modx->context->get('key') != 'mgr') {
$force_ssl = (int)$modx->getOption('force_ssl', 0, 0);
$force_ssl_ids = $modx->getOption('force_ssl_ids', null, null);
if ($force_ssl_ids) {
$force_ssl_id = explode(",", $force_ssl_ids);
}
//return if secure or no ssl
if (!$force_ssl && !$force_ssl_ids) return;
$cID = $modx->resourceIdentifier;
$site_url = $modx->getOption('site_url', '', '');
$ssl_site_url = $modx->getOption('ssl_site_url', '', $site_url);
if ($site_url == $ssl_site_url) {
$sslUrl = $modx->makeUrl($cID,'','','https');
} else {
$sesName = session_name();
$sesId = session_id();
$sslUrl = 'https:' . '//' . $ssl_site_url.$modx->makeUrl($cID,array($sesName=>$sesId),'','');
}
if ($force_ssl) {
$modx->sendRedirect($sslUrl);
} elseif (in_array($cID, $force_ssl_id)) {
$modx->sendRedirect($sslUrl);
}
} else {
$force_ssl_manager = $modx->getOption('force_ssl_manager', '', '');
if (empty($force_ssl_manager)) return;
//$sslUrl = $modx->makeUrl(-1,'','','https');
//if ($force_ssl_manager == 'all') {
// $modx->sendRedirect($sslUrl);???
//} elseif ($force_ssl_manager == 'login' && $modX->user->isAuthenticated('mgr') ) {
// $modx->sendRedirect($sslUrl);???
//}
}
@zaenal
Note: I think many of us using shared SSL, and it should be more easier if (just "if") modx->makeURL() function could recognize site_url for http and ssl_site_url for https.
[ed. note: lokamaya last edited this post 14 years, 11 months ago.]