![](/img/trans.png)
[英]Google chrome redirect from https to http in my yii2 application
[英]Redirect http to https in Yii2 .htaccess
htaccess 文件將我的高級 Yii2 應用程序重定向到前端 index.php 文件,因為已經有一個 .htaccess 文件。 其中有以下幾行..
這是現在我在根目錄下的 .HTACCESS
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]
現在我搜索了互聯網,如果我想重定向到 https,我發現了這個,那么我必須添加這個。
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
所以我加了這樣....
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]
然后它在沒有任何js和css的情況下加載網站......而且它也在http中。 但它需要的內容應該在 https 中。
我確實理解這是因為我的另一個重寫聲明。 但我在這方面不是很專業。 請提出一些建議。
在這里,如果單獨使用這個東西,它可以完美運行,沒有任何問題。
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
它會將我的 http 發送到 https。 所以它完美地工作。
或者如果這是單獨的
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ frontend/web/$1 [L]
此規則告訴從 [.htaccess Directory/frontend/web/] 目錄執行 index.php 文件。 表示執行 frontend/web/index.php 。 這樣做非常重要,因為這是根據框架結構進行的。
所以我必須將這兩個規則結合在一起並構建一個適用於這兩種情況的 .htaccess。
結論
所以我的 .HTACESS 應該告訴服務器我們必須在 https 中運行 [.HTACES DIR/frontend/web/index.php]。
更新問題
這是我的 index.php 所在的 frontend/web/ 文件夾下的 .htaccess
這是在 index.php 所在的 root/frontend/web 文件夾中。
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
我必須在這里添加 https 嗎?
最簡單的方法可能是在/config/web.php
設置on beforeRequest
事件處理程序,如下所示:
...
'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();
}
},
...
你root/frontend/web/.htaccess
應該是這樣的:
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]
請記住,子目錄.htaccess
始終優先於父目錄.htaccess
。
另一種方法是更改端口 80 (HTTP) 上的虛擬主機:
<VirtualHost *:80>
ServerName mywebsite.com
Redirect permanent / https://mywebsite.com
</VirtualHost>
然后為端口 443 (HTTPS) 設置一個單獨的虛擬主機。
假設是 Debian(-fork) Linux 服務器,您必須將上述內容放在/etc/apache2/sites-available/myname.conf
。 完成后,使用sudo a2ensite myname.conf
(如果失敗,您可以自己在/etc/apache2/sites-enabled/
創建符號鏈接,但盡量不要)。 現在使用“sudo service apache2 restart”重新啟動apache。
當我把這個放在 root .htaccess 中時。 它也適用於我。
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ frontend/web/$1 [L]
我在下面使用了這個代碼。
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]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.