Friday , December 20 2024

How to add 301 redirect to https for Yii2 without .htaccess

I’ve tested different redirect options for .htaccess, but sites keep giving “redirected you too many times” error. Here is the working code to solve the problem.

In the file /config/web.php you need to find:

'bootstrap' => [
		'log',
		'app\components\Bootstrap',
	],

and place below:

'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();
       }
},

About admin

Hi there! My name is Iryna, I am a web developer from Ukraine. While working on freelance projects I often face different technical issues and in this blog I publish articles on how to solve them. I am also interested in digital drawing and in this blog you will find brief tutorials that helped me and hopefully can help you as well.

Check Also

Css styles for WordPress admin in functions.php

If you need to add individual styles, and they are only a couple of lines, …

Leave a Reply

Your email address will not be published. Required fields are marked *