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

0 like 0 dislike
54 views
Good afternoon, dear experts!
There is a website on YII2 basic
htaccess in the site root:
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 the web folder:
RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . index.php

Questions:
1) how to correctly do a redirect from http to https for all pages
2) how to correctly remove the www from the address bar - now the address with www will redirectid {sitename.ru}//web/index.php and leads to 404 error.
Thanks in advance for your answers!
asked by | 54 views

2 Answers

0 like 0 dislike
Thank you for your attention!
The problem was solved in the following way: .htaccess, which is at the root of public folders has been 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 in the web folder is left unchanged:
RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . index.php

And began to work:
1) for all URLS where there is no https - redirects to https
2) for all URLS where there is a WWW - removes it
answered by
0 like 0 dislike
Good evening.
Try to add to web/.htaccess
RewriteCond %{HTTPS} !=on RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

If Your project is in a subdirectory, https://%{SERVER_NAME}/$1 [R,L] need to add a subdirectory.
answered by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
24,509 questions
45,932 answers
0 comments
345 users