iT邦幫忙

0

關於自動把http請求變成https的方法 (感謝 echochio、跑得快、yoching 協助解決)

小弟在上課的時候有學到,可以使用
「rewrite ^(.*) https://$server_name$1 permanent;」

將http變成https訪問,後來自己想多做幾組域名的時候發現,不知道為什麼一直導向成scottAAA.scott01.com

server {
  listen 80;
  server_name
    scottAAA.scott01.com
    scottBBB.scott01.com
    scottCCC.scott01.com
    scottDDD.scott01.com;

rewrite ^(.*) https://$server_name$1 permanent;

不管是輸入哪一個網址,最後都會顯示成scottAAA.scott01.com
意思是說
我複製 scottBBB.scott01.com 貼上網址 跳轉 > https://scottAAA.scott01.com/
然後如果複製 https://scottBBB.scott01.com/ 就沒發生跳轉的事情了

後來一直嘗試之後,才發現,我將scottBBB.scott01.com 改到 server_name的第一航位置時候,就改跳scottBBB.scott01.com了也就是說會變成這樣

server {
  listen 80;
  server_name
    scottBBB.scott01.com
    scottAAA.scott01.com
    scottCCC.scott01.com
    scottDDD.scott01.com;

rewrite ^(.*) https://$server_name$1 permanent;

因此斷定是「rewrite ^(.*) https://$server_name$1 permanent;」這條代碼在做怪

但是google了一下,試了幾個方法,都無法解決這個問題,不知道邦友大神,有沒有人遇過類似的問題,感謝指教

看更多先前的討論...收起先前的討論...
echochio iT邦高手 1 級 ‧ 2019-01-31 08:05:21 檢舉
好像是
RewriteEngine On
RewriteCond %{HTTPS} off
# RewriteCond %{SERVER_PORT} !=443
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R,L]
跑得快 iT邦新手 3 級 ‧ 2019-01-31 09:10:28 檢舉
這個參考看看?
https://httpd.apache.org/docs/2.4/rewrite/avoid.html
踏雪尋梅 iT邦研究生 5 級 ‧ 2019-01-31 09:22:06 檢舉
謝謝你,不過我昨天睡前有找到一條代碼
「rewrite ^(.*) https://$host$1 permanent;」

經過無痕測試後,似乎解決了,不知道是否正確,跟您核對一下
這是我目前用的設定,你這因該是nginx才對

server {
listen 123.123.123.123:80;
server_name 123.net *.123.net *.test.123.net;
# 導向至 HTTPS
rewrite ^(.*) https://$host$1 permanent;
}

看起來就是你後面找到的
踏雪尋梅 iT邦研究生 5 級 ‧ 2019-02-01 14:12:59 檢舉
謝謝你們,這兩天忘了來感謝跟結案,有順利解決了
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答