Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Redirect to Subdomain for SSL


Reply


2 Posts   1296 Views

Avatar
eceers

Community Member, 24 Posts

21 October 2010 at 2:40pm

My client has set up a ssl domain at https://secure.domain.com and the general site is on the normal address of http://www.domain.com.

My question is how do I change the baseURL rather than just the protocol?

I'm hoping this is going to be a quick one. I've been looking around for a bit and haven't been able to find much, at least not what I was searching for :(

Any help would be greatly appreciated.

Avatar
eceers

Community Member, 24 Posts

22 October 2010 at 12:31pm

I have worked it out in a round about way.

I created an extra function along the lines as one shown here http://doc.silverstripe.org/ssl?s[]=secure&s[]=pages

   public function init() {

      parent::init();
      
      if (!Director::isDev()) {
    if ( !$this->SecurePage()) {
    $this->force_HTTP();
    } else {
       $this->force_HTTPS();
    }
      }
}

// if we are on https, redirect to the http version of this page's URL
function force_HTTP() {
$page_url = Director::absoluteURL( $_SERVER['REQUEST_URI'] );
$https_regex = '{^https:}';
if ( preg_match( $https_regex, $page_url ) ) {
$new_url = preg_replace( $https_regex, 'http:', $page_url );
Director::redirect('http://www.domain.com'.$this->Link());
}
}
function force_HTTPS() {
$page_url = Director::absoluteURL( $_SERVER['REQUEST_URI'] );
$https_regex = '{^http:}';
if ( preg_match( $https_regex, $page_url ) ) {
$new_url = preg_replace( $https_regex, 'https:', $page_url );
Director::redirect('https://secure.domain.com'.$this->Link());
}
}


Declaring in the class

function SecurePage() {
return false;
}

It may not be 100% right but it appears to work the way I need it to.

If anyone has any suggestions to improve would be greatly appreciated.