0

I try to run my local copy of my yii2 site with https. I use this in config to force http url to https

    'on beforeRequest' => function ($event) {
    if(!Yii::$app->request->isSecureConnection){
        $url = Yii::$app->request->getAbsoluteUrl();
        $url = str_replace('http:', 'https:', $url);
        Yii::$app->getResponse()->redirect($url);
        Yii::$app->end();
    }
},

The only url I can reach is the home page i.e. a bare url such as example.ext Other URLs give

Not Found The requested URL /site/index was not found on this server.

When removing the 'onbeforerequest' in the config, I can reach every http URL. Question: why https URLs become unreachable?

jaaf64
  • 23
  • 1
  • 6
  • If I understand what you mean, why don't you use a **.htaccess** file for such thing? – Francis Ngueukam Nov 04 '17 at 17:18
  • Note if you're using >= Yii 2.0.13 via ssl proxy/termination, see: https://www.yiiframework.com/doc/guide/2.0/en/runtime-requests#trusted-proxies – ldg Apr 02 '18 at 23:24

1 Answers1

0

Eventually I made out that there was no url rewrite for pretty url in the virtualhost litening to 443 port. Adding the recommended rewrite rule in it solved the problem.

@stfsngue: Thank you for comment Do you see any particular reason for preferring .htacces to 'onbeforeRequest' to force https?

jaaf64
  • 23
  • 1
  • 6