How to create absolute url on Yii https?
Asked Answered
E

5

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 ?

Elke answered 4/11, 2013 at 4:34 Comment(0)
B
21

Try this

Yii::app()->createAbsoluteUrl('site/index', array(), 'https');
Bobseine answered 4/11, 2013 at 5:34 Comment(1)
but i have added this in 75 places in my site.. is there a single shot to do this ?Elke
C
5

Try to change host

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

Cohabit answered 26/11, 2015 at 10:46 Comment(1)
i tried and its work, but it is hostInfo not HostInfoIndivertible
H
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]
Heartache answered 4/11, 2013 at 7:12 Comment(6)
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.Gabrielegabriell
@ahonnecke The Application is not the place to handle http & https. It's clearly webserver related. Also it is the simple solution.Heartache
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.Gabrielegabriell
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.Indubitability
@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).Heartache
Okay, I guess thinking about it some more, my comment is only valid if you are doing ssl termination on haproxy.Indubitability
T
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', ), ... ),

Tile answered 5/9, 2014 at 10:33 Comment(3)
This doesn't appear to work when creating a redirect in a controller.Indubitability
Do you have absolute rules configured in the urlManager component?Jampacked
No, I don't see options for that in: yiiframework.com/doc/api/1.1/CUrlManager or yiiframework.com/doc/guide/1.1/en/topics.urlIndubitability
I
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.

Indubitability answered 18/9, 2015 at 2:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.