We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 23571
    • 223 Posts
    I have been trying to get this approach (http://modxcms.com/forums/index.php/topic,56650.20.html) to work for forcing a single MODx document to https. In addition to creating a plugin with the code below, I have checked OnLoadWebDocument in the System Events tab. I have also created a TV named ’encryption’ with a Check Box Input Type, Input Option Values ’On==1’ and Default Value ’0’. Once I ’check’ the TV on a document I am redirected to the site start page instead of the appropriate https URL for the corresponding document.

    Any assistance would be appreciated.

    if($modx->event->name == 'OnLoadWebDocument') {
       
        $encryption_value = $modx->resource->getTVValue('encryption');
        
        //finally the value
        $encryption = false;
        if($encryption_value == 1){
          $encryption = true;
        }
        
        $ssl = false;
        if($_SERVER['HTTPS'] == 1)  {
          $ssl = true;
        }
        elseif($_SERVER['HTTPS'] == 'on'){
          $ssl = true;
        }
        elseif($_SERVER['SERVER_PORT'] == 443) {
          $ssl = true;
        }
       
        // switch between http / https if necessary
        // if $secureserver === $insecureserver (eg you are testing locally)
        // then comment out this code block to avoid infinite recursion
        
        // if SSL is on and we are about to acccess an unsecure page then redirect
        if($ssl && !$encryption) {
         
          $url = $modx->makeUrl($modx->resource->get('id'), '', '', 'http');
          $modx->sendRedirect($url);
        }
        // if SSL off and we are about to access a secure page then redirect
        else if(!$ssl && $encryption) {
         
          $url = $modx->makeUrl($modx->resource->get('id'), '', '', 'https');
          $modx->sendRedirect($url);
        }
      }
      • 23571
      • 223 Posts
      Amazing what you can fix when you re-copy. This is solved, the original code worked.
        • 6228
        • 249 Posts
        Be careful with that approach - the redirect will drop any query parameters attached to the url.

        I use something like this (using a TV to choose between http and https schemes):
        // Is this currently a secure connection?
        $ssl = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on';
        // Get the https TV that may be set for this page
        $val = $modx->resource->getTVValue('HTTPS');
        
        if ($val == 'Yes' && !$ssl) {
        	$redirect = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        	$modx->sendRedirect($redirect);
        	exit();
        
        } elseif ($val != 'Yes' && $ssl) {
        	$redirect = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        	$modx->sendRedirect($redirect);
        	exit();
        }

          lo9on.com

          MODx Evolution/Revolution | Remote Desktop Training | Development