O
O
Oleg2018-11-11 23:50:27
Yii
Oleg, 2018-11-11 23:50:27

How to make a redirect from http to https for a site on YII2 (basic) using .htaccess?

Good afternoon, dear experts!
There is a site on YII2 basic
htaccess at the root of the site:

Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on

RewriteCond %{REQUEST_URI} !^/(web)
RewriteRule ^assets/(.*) /web/assets/$1 [L]
RewriteRule ^css/(.*) /web/css/$1 [L]
RewriteRule ^scss/(.*) /web/scss/$1 [L]
RewriteRule ^js/(.*) /web/js/$1 [L]
RewriteRule ^img/(.*) /web/img/$1 [L]
RewriteRule ^fonts/(.*) /web/fonts/$1 [L]
RewriteRule (.*) /web/$1

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /web/index.php

htaccess in web folder:
RewriteBase /

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

Questions:
1) how to correctly redirect from http to https for all pages
2) how to correctly remove www from the address bar - now the address from www is redirected to {sitename.ru}//web/index.php and leads to a 404 error.
Thanks in advance for your replies!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Oleg, 2018-11-12
@volego

Thank you for your attention!
The problem was solved as follows: .htaccess, which is in the root of the public folder, edited:

Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on

#enable HTTPS only
#RewriteCond %{HTTPS} !=on
#RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

#remove WWW
#RewriteCond %{HTTP_HOST} ^www\.site\.ru$ [NC]
#RewriteRule ^(.*)$ https://site.ru/$1 [R=301,L]

RewriteCond %{REQUEST_URI} !^/(web)
RewriteRule ^assets/(.*) /web/assets/$1 [L]
RewriteRule ^css/(.*) /web/css/$1 [L]
RewriteRule ^scss/(.*) /web/scss/$1 [L]
RewriteRule ^js/(.*) /web/js/$1 [L]
RewriteRule ^img/(.*) /web/img/$1 [L]
RewriteRule ^fonts/(.*) /web/fonts/$1 [L]
RewriteRule (.*) /web/$1

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /web/index.php

.htaccess, which was left unchanged in the web folder:
RewriteBase /

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

And everything began to work fine:
1) for all URLs where there is no https - redirect to https
2) for all URLs where there is WWW - removes it

D
Dmitry, 2018-11-12
@slo_nik

Good evening.
Try adding to web/.htaccess

RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

If your project is in a subdirectory, then https://%{SERVER_NAME}/$1 [R,L]you need to add the name of the subdirectory.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question