7

I am creating a website which runs on https.. But when i create absolute url using

echo    Yii::app()->createAbsoluteUrl('site/index');

it always return http://mydomainname.com/site/index.

my expected output is https://mydomainname.com/site/index.

How can i create a url with https ?

Harikrishnan
  • 9,688
  • 11
  • 84
  • 127
Mahesh Eu
  • 515
  • 1
  • 6
  • 21

5 Answers5

21

Try this

Yii::app()->createAbsoluteUrl('site/index', array(), 'https');
Neophile
  • 1,519
  • 12
  • 15
4

Edit .htaccess file in your project folder and add these lines.It will Redirect all http traffic to https.

RewriteEngine On
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://mydomainname.com/$1 [R,L]
Harikrishnan
  • 9,688
  • 11
  • 84
  • 127
  • This does not answer the question "How can i create a url with https". This is also apache specific. This should not be the accepted answer or the question should be rephrased. – Ashton Honnecke Mar 19 '15 at 19:30
  • 1
    @ahonnecke The Application is not the place to handle http & https. It's clearly webserver related. Also it is the simple solution. – Harikrishnan Mar 20 '15 at 03:34
  • All the things you say are correct for the majority of situations. Your answer is not helpful for people who come along later and are looking for the *actual* answer to the question that was asked. – Ashton Honnecke Apr 02 '15 at 20:21
  • This means that every time you redirect the user you are redirecting them to the HTTP version of the site and then once they fetch that they are redirected AGAIN to the HTTPS version. That's an extra wasted redirect. – Jason Axelson Sep 18 '15 at 00:02
  • @JasonAxelson There is no redirection to http. The browser request comes to http by default (port 80). Then webserver redirects those http requests to https (port 443). – Harikrishnan Sep 18 '15 at 03:19
  • Okay, I guess thinking about it some more, my comment is only valid if you are doing ssl termination on haproxy. – Jason Axelson Sep 18 '15 at 19:52
4

Try to change host

'components' => array( ... 'request' => array( 'HostInfo' => 'https://mydomainname.com', ), ... ),

Mayur Bhamre
  • 191
  • 1
  • 10
0

The "single shot" way is to set https:// in the baseUrl parameter from the configuration:

... 'components' => array( ... 'request' => array( 'baseUrl' => ' https://mydomainname.com/site', ), ... ),

Radu Dumbrăveanu
  • 1,266
  • 1
  • 22
  • 32
0

You're best off creating a custom UrlManager implementation as described here:

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

The benefit is that your users won't have to suffer through needless double redirects every time you have a genuine redirect.

Jason Axelson
  • 4,485
  • 4
  • 48
  • 56