Redirect http to https in Yii2.htaccess

An htaccess file that redirects my advanced Yii2 application to the frontend index.php file so that there is already a .htaccess file. which has the following lines.

It is now right now .HTACCESS in the root directory

Options -Indexes <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_URI} !^public RewriteRule ^(.*)$ frontend/web/$1 [L] </IfModule> # Deny accessing below extensions <Files ~ "(.json|.lock|.git)"> Order allow,deny Deny from all </Files> # Deny accessing dot files RewriteRule (^\.|/\.) - [F] 

now i searched the internet and i found this if i want to redirect to https then i have to add this.

 RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} 

so I added like this ....

 Options -Indexes <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} RewriteCond %{REQUEST_URI} !^public RewriteRule ^(.*)$ frontend/web/$1 [L] </IfModule> # Deny accessing below extensions <Files ~ "(.json|.lock|.git)"> Order allow,deny Deny from all </Files> # Deny accessing dot files RewriteRule (^\.|/\.) - [F] 

then it loads the website using any js and css ... and is also in http. But the content that it requires must be in https.

I understand that this is because of my other rewrite statement. But I'm not so good at it. Please make an offer.

Here this thing, if it is one, works fine without any problems.

  RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} 

He will send my http to https. So it works.

Or if this is one

  RewriteCond %{REQUEST_URI} !^public RewriteRule ^(.*)$ frontend/web/$1 [L] 

This rule reports that execute the index.php file from the [.htaccess Directory / frontend / web /] directory. The tool runs the /web/index.php interface. It is very important to do this here, since it corresponds to the structure of the structure.

So, I need to combine these two rules together and create a .htaccess that will work for both scenarios.

Conclusion

So, my .HTACESS should tell the server that we should run [.HTACESS DIR / frontend / web / index.php] in https.

UPDATE TO QUESTION

this is .htaccess in the interface / web / folder where my index.php is

And this is in the root / frontend / web folder where index.php exists.

 RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . index.php 

Do I need to add https here?

+4
source share
5 answers

You root/frontend/web/.htaccess should look something like this:

 RewriteEngine on RewriteBase /frontend/web/ RewriteCond %{HTTPS} off RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^ index.php [L] 

Just remember that the .htaccess subdirectory always takes precedence over the parent .htaccess directory.

+6
source

The easiest way is to set the on beforeRequest event handler in /config/web.php as follows:

 ... 'bootstrap' => ['log'], '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(); } }, ... 
+6
source

Another approach would be to change the virtual host on port 80 (HTTP):

 <VirtualHost *:80> ServerName mywebsite.com Redirect permanent / https://mywebsite.com </VirtualHost> 

and then have a separate virtual host for port 443 (HTTPS).

Assuming the Linux Debian server (-fork) you will need to put above in /etc/apache2/sites-available/myname.conf . After you have done this, use sudo a2ensite myname.conf (if this fails, you can create a symbolic link in /etc/apache2/sites-enabled/ yourself, but don't try). Now restart apache with 'sudo service apache2 restart'.

+1
source

In the root .htaccess when I install it. This also works for me.

 RewriteEngine on RewriteCond %{HTTPS} off RewriteRule ^(.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L] RewriteCond %{REQUEST_URI} !^public RewriteRule ^(.*)$ frontend/web/$1 [L] 
+1
source

I used this code below.

 RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} ^www.(.*) [NC] RewriteRule ^(.*)$ http://%1/$1 [R=301,L] RewriteCond %{HTTPS} on RewriteCond %{HTTP_HOST} ^www.(.*) [NC] RewriteRule ^(.*)$ https://%1/$1 [R=301,L] 
0
source

All Articles