鍍金池/ 問答/PHP/ php的yii2框架,apache配置.htaccess使http轉(zhuǎn)向https

php的yii2框架,apache配置.htaccess使http轉(zhuǎn)向https,默認請求指向根目錄下的index.php

yii2框架的advance版,框架開啟了URL美化:

'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules'=>[
            "<controller:\w+>/<id:\d+>"=>"<controller>/view",
            "<controller:\w+>/<action:\w+>"=>"<controller>/<action>"    
        ],
    ],

使用的apache服務(wù)器,項目backend目錄的web下配置.htaccess:
原來的配置是http訪問的方式,可以正常訪問:

    Options +FollowSymlinks
    IndexIgnore */*
    RewriteEngine On

    # if a directory or a file exists, use it directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    # otherwise forward it to index.php
    RewriteRule . index.php

    RewriteRule \.svn\/ /404.html
    RewriteRule \.git\/ /404.html
加了阿里云的免費ssl證書后,修改為如下后訪問跳轉(zhuǎn)到https了卻是404:
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
        RewriteCond %{HTTPS} !=on
        RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

        # BEGIN WordPress
        RewriteRule ^index\.php$ - [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /index.php [L]
    </IfModule>
    
**ssl證書配置是:**
   Listen 443

   <VirtualHost *:443>
        DocumentRoot "C:\WWW\demo1\backend\web"
        ServerName test.ssl1.com
        ServerAlias test.ssl1.com
        <Directory "C:\WWW\demo1\backend\web">
          Options FollowSymLinks ExecCGI
          AllowOverride All
          Order allow,deny
          Allow from all
          Require all granted
        </Directory>

        SSLEngine on
        SSLProtocol all -SSLv2 -SSLv3
        SSLCipherSuite         HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM
        SSLHonorCipherOrder on

        SSLCertificateFile "C:\Program Files\Apache\cert\ssl1\public.pem"
        SSLCertificateKeyFile "C:\Program Files\Apache\cert\ssl1\214389064350038.key"
        SSLCertificateChainFile "C:\Program Files\Apache\cert\ssl1\chain.pem"
    </VirtualHost>
網(wǎng)上找了好多資料,求教各位大神怎么解決,不勝感激
回答
編輯回答
不二心

web目錄下的.htaccsee完整配置,不論是一級域名還是二級域名都可以訪問,已經(jīng)經(jīng)過測試:

Options +FollowSymlinks
IndexIgnore */*
RewriteEngine On

RewriteCond %{SERVER_PORT} 80
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R,L]

RewriteCond %{SERVER_PORT} !^443$ 
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R]

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php

RewriteRule \.svn\/ /404.html
RewriteRule \.git\/ /404.html
2017年4月20日 10:44
編輯回答
情殺
 RewriteCond %{SERVER_PORT} !^443$ 
 RewriteRule (.*) https://%{SERVER_NAME}/$1 [R]

以下補充

實現(xiàn)http轉(zhuǎn)https原理就是
原先訪問80端口的轉(zhuǎn)443端口(也可以是其他端口,這個看你)
代碼角度就是

http://uri
/*變成下面*/
https://uri

這個可以基于服務(wù)器的重定向功能
你的服務(wù)器是apache的
以下的內(nèi)容都是基于apache說明的
1首先必須開啟重寫規(guī)則這個模塊
LoadModule rewrite_module modules/mod_rewrite.so
2寫規(guī)則
所有重定向內(nèi)容從RewriteEngine On開始看.
但是有一點,規(guī)則可以寫在apache的httpd文件,
.htaccess這個實現(xiàn) 是基于httpd文件中 AllowOverride的值 設(shè)置允許下才生效,重寫功能實現(xiàn)相當(dāng)于http文件的一個快捷方式,可以這么粗糙理解

clipboard.png

這個是apache加載規(guī)則一個原理圖

apache加載第一個規(guī)則后
RewriteRule '正則匹配到內(nèi)容' '替換后uri' //這種格式
如果有 RewriteCond //相當(dāng)于進入一層if判斷
至此完成一個規(guī)則加載,獲得替換后的uri 作為值 進入下一規(guī)則匹配

實現(xiàn)http轉(zhuǎn)的https的規(guī)則代碼就這么點,代碼一開始已經(jīng)貼出來了.
你之所以不能生效在你的服務(wù)器上,是因為其他規(guī)則干擾.
這是你可以找下你apache/log文件.看看錯在哪里
可以在開發(fā)模式中,你自己模擬下
LogLevel alert rewrite:trace等級 將這個等級調(diào)高點,生產(chǎn)模式的話則不要這么做不然會產(chǎn)生大量日志
然后在日志中分析,調(diào)整你規(guī)則位置 和匹配代碼

下面我示范下我的

clipboard.png

clipboard.png

其中我把等級調(diào)為8,可以看到整個規(guī)則匹配的走向流程
最后說一點,遇到翻官網(wǎng)文檔是最齊全的,別人寫的也是基于官網(wǎng)修改的,
至于語言障礙,你可以用谷歌翻譯下


在補充一點httpd文件修改必須重啟apache
.htaccess 修改不需要
還有瀏覽器會緩存你的重定向,這是你要試試其他url參數(shù).
ps:有可能你寫對了,但是讀取了上一次的,這點我被坑過很慘T_T

2018年7月17日 19:46