在 Apache 裡,可以在 .htaccess 或 httpd.conf 這兩個檔案設定轉址 (URL rewrite, 網址重寫),這次收到的任務是希望將特定的網頁(例如使用者登入、個人資料填寫等等),轉向 HTTPS。 

原本打算直接在 HTML code 裡把 <a> 標籤裡的 href 屬性直接設成 https 開頭,就此交差結案,不過實際操作一陣子後,發現只要 user 有進入過一次 "https://" 開頭的頁面,接下來的相對連結也都通通會轉向 HTTPS。但某些功能擔心會在 HTTPS 下出問題,尤其之前檔案下載頁會出些狀況:[PHP] SSL 網站在 IE 會發生下載錯誤的問題修正,所以還是決定找點解法來設定轉址。

研究了一陣子發現 Apache 的 Rewrite 模組有 "Skip" 這個 tag,設定 [S=N] (N 為要跳過的規則數量),就可以讓 Rewrite 對特定的條件生效,於是有了以下設定。

以我自己的例子是同一網站有 80 port (HTTP) & 443 port (HTTPS) 的 Virtual Hosts 設定,如此一來要在 80 port 的區段做以下的轉址設定:

<VirtualHost *:80>
    ServerAdmin ........
    ServerName ........
    ServerAlias ........
    DocumentRoot "C:/Apache/htdocs/"
    
    <Directory "C:/Apache/htdocs/">
        Options ExecCGI FollowSymLinks
        AllowOverride None
        Require all granted
    
        RewriteEngine on
    
        # 這是此次設定前就有的 URL rewrite 規則,因為我們之前有在同一台網站上應用過很多組 domain name 
        # 設這個的目的是希望當使用者透過指定的 domain name 以外的路徑連入的頁面,都能轉址到固定的一組 domain name

        RewriteCond %{HTTP_HOST} !(^127\.0\.0\.1)|(^www.test.tw)
        RewriteRule ^(.*)$ http://www.test.tw/$1 [R=301,L]

        # 當網址含有特定字串 (這邊的例子是 "user_functions") 時,表示為希望轉向 HTTPS 的網頁
        RewriteCond %{HTTPS} !on
        RewriteCond %{REQUEST_URI} ^.*/user_functions/.*$ [NC]  
        RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]
    </Directory>
</VirtualHost>

 

 這樣設過以後,網址含有 user_functions 時,就會轉向 HTTPS,但是還要另外在 HTTPS 的區段做些設定,讓不慎用 HTTPS 誤闖進來的 user 可以走回去 HTTP。

<VirtualHost *:443>
    ServerAdmin ...
    ServerName ...
    ServerAlias ...
    DocumentRoot "C:/Apache/htdocs/"
    
    <Directory "C:/Apache/htdocs/">
        Options ExecCGI FollowSymLinks
        AllowOverride None
        Require all granted
    
        RewriteEngine on
        RewriteCond %{HTTP_HOST} !^www\.test\.tw
        RewriteRule ^(.*)$ https://www.test.org.tw/$1 [R=301,L]
        # 當網址含有特定字串 (這邊的例子是 "user_functions") 時,表示為希望轉向 HTTPS 的網頁,跳過底下的四個 rewrite 條件
        RewriteCond %{REQUEST_URI} ^.*/user_functions/.*$ [NC]
        RewriteRule ".?" "-" [S=4]
        # 當網址為 JavaScript / JS / 圖檔等檔案,仍希望保持原來的連線方式(是 HTTP 就走 HTTP、是 HTTPS 就走 HTTPS),所以也跳過底下的三個設定
     
   RewriteCond
%{REQUEST_URI} \.js|\.css|\.(jpg|jpeg)|\.gif|\.png [NC]
        RewriteRule ".?" "-" [S=3]
        # 若網址未含有以下內容:
        #   - user_functions (想要轉到 HTTPS 的網址)
        #   - .js (JavaScript)
        #   - .jpg/.gif/.png (圖檔)
        #   - .css (CSS 樣式檔)

        # 表示該網址為普通網頁,轉向 HTTP
        RewriteCond %{SERVER_PORT} ^443$  [OR]
        RewriteCond %{HTTPS} on
        RewriteCond %{REQUEST_URI} ^.*(?!\/reward\/).*$|(?!\.js)|(?!\.css) [NC]
        RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R]
        RewriteRule ^(.*)$ http://www.test.tw/$1 [R=301,L]
    </Directory>
</VirtualHost>

 

 設定完畢後記得重啟 Apache,才會生效哦。

 

 

We can setup .htaccess or httpd.conf to force speicific URL redirect (URL rewrite). 

If we only use hyperlink on the web page to lead user to HTTPS page, they will use HTTP protocol to browe our web site after they once come to the HTTPS link.

Using Apache mod_rewrite module, we can redirect user to use HTTPS protocol in speicific page, and redirect they to HTTP protocol in normol page.

All the configuration of httpd.conf shows above. 

After changing .htaccess or httpd.conf, don't forget restart Apache. :-)

arrow
arrow

    小攻城師 發表在 痞客邦 留言(0) 人氣()