Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8799561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:21:26+00:00 2026-06-14T00:21:26+00:00

Im using a code to separate pages that is HTTPS and HTTP in my

  • 0

Im using a code to separate pages that is HTTPS and HTTP in my website

The problem is: When Im on HTTP, links to HTTPS no have WWW and vice versa.
I did not find the problem in the script.

public function createUrl($route, $params = array(), $ampersand = '&')
{
    $url = parent::createUrl($route, $params, $ampersand);

    // If already an absolute URL, return it directly
    if (strpos($url, 'http') === 0) {
        return $url;  
    }

    // Check if the current protocol matches the expected protocol of the route
    // If not, prefix the generated URL with the correct host info.
    $secureRoute = $this->isSecureRoute($route);
    if (Yii::app()->request->isSecureConnection) {
        return $secureRoute ? $url : 'http://' . Yii::app()->request->serverName . $url;
    } else {
        return $secureRoute ? 'https://' . Yii::app()->request->serverName . $url : $url;
    }
}

public function parseUrl($request)
{
    $route = parent::parseUrl($request);

    // Perform a 301 redirection if the current protocol 
    // does not match the expected protocol
    $secureRoute = $this->isSecureRoute($route);
    $sslRequest = $request->isSecureConnection;
    if ($secureRoute !== $sslRequest) {
        $hostInfo = $secureRoute ? 'https://' . Yii::app()->request->serverName : 'http://' . Yii::app()->request->serverName;
        if ((strpos($hostInfo, 'https') === 0) xor $sslRequest) {
            $request->redirect($hostInfo . $request->url, true, 301);
        }
    }
    return $route;
}

private $_secureMap;

/**
 * @param string the URL route to be checked
 * @return boolean if the give route should be serviced in SSL mode
 */
protected function isSecureRoute($route)
{
    if ($this->_secureMap === null) {
        foreach ($this->secureRoutes as $r) {
            $this->_secureMap[strtolower($r)] = true;
        }
    }
    $route = strtolower($route);
    if (isset($this->_secureMap[$route])) {
        return true;
    } else {
        return ($pos = strpos($route, '/')) !== false 
            && isset($this->_secureMap[substr($route, 0, $pos)]);
    }
}

}

Code adapted from: http://www.yiiframework.com/wiki/407/url-management-for-websites-with-secure-and-nonsecure-pages/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:21:27+00:00Added an answer on June 14, 2026 at 12:21 am

    It’s better to manage this at the controller level using filters.

    In your components directory setup 2 filters HttpsFilter and HttpFilter as follows:-

    class HttpsFilter extends CFilter {
    
        protected function preFilter( $filterChain ) {
            if ( !Yii::app()->getRequest()->isSecureConnection ) {
                # Redirect to the secure version of the page.
                $url = 'https://' .
                    Yii::app()->getRequest()->serverName .
                    Yii::app()->getRequest()->requestUri;
                    Yii::app()->request->redirect($url);
                return false;
            }
            return true;
        }
    
    }
    

    and

    class HttpFilter extends CFilter {
    
        protected function preFilter( $filterChain ) {
            if ( Yii::app()->getRequest()->isSecureConnection ) {
                # Redirect to the secure version of the page.
                    $url = 'http://' .
                    Yii::app()->getRequest()->serverName .
                    Yii::app()->getRequest()->requestUri;
                    Yii::app()->request->redirect($url);
                return false;
            }
            return true;
        } 
    }
    

    then in each controller force https using the filters, optionally by action:

    class SiteController extends Controller {
    
        public function filters()
        {
            return array(
                'https +index', // Force https, but only on login page
            );
        }
    }
    

    Edit: if the filters() function above doesn’t seem to work for you, instead try

    return array(
               array('HttpsFilter +index'), // Force https, but only on login page
           );
    

    See http://www.yiiframework.com/doc/guide/1.1/en/basics.controller#filter (and comments on it).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, as per a separate question that I asked I'm using the following code
I'm using Code First with EF 5. Currently I have SPs that return just
I am using a separate print css on our website. It's been requested that
I'm using the code below to separate a group of strings separated by a
I am using code in Matlab that writes xy coordinates and an associated z-value
I'm using code from other c# app and I can see that in WP7
I'm using Code::Blocks, and I have installed wxWidgets 2.8. Everything worked ok, I have
I'm optimizing my website according to Google's site optimization standards: http://code.google.com/speed/page-sp...mageDimensions For those who
I wrote a simple HTTP sever that serves some HTML. Here's the code :
I am using jqPlot javascript library ( http://www.jqplot.com/ ) for graphs and charts in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.