http自動轉https及www自動轉非www的網址同步實現問題

1.
當我們在.htaccsee中設置
RewriteCond %{HTTP_HOST} ^(www\.example\.com\.tw)(:80)? [NC]
RewriteRule ^(.*) http://example.com.tw/$1 [R=301,L]
訪客透過輸入連結進入網站
輸入http://www.example.com.tw會自動轉跳至http://example.com.tw

2.
然而今天如果加裝了ssl,想讓訪客自動跳轉到https://example.com.tw
.htaccess就要重新設置
RewriteCond %{HTTP_HOST} ^(www\.example\.com\.tw)(:80)? [NC]
RewriteRule ^(.*) https://example.com.tw/$1 [R=301,L]
但這樣設置頂多
訪客今天輸入http://www.example.com.tw會自動轉跳到https://example.com.tw
訪客輸入http://example.com.tw依然會停留在http://example.com.tw

把.htaccess內容設置成
RewriteCond %{HTTP_HOST} ^(www\.example\.com\.tw)(:80)? [NC]
RewriteRule ^(.*) https://example.com.tw/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(example\.com\.tw)(:80)? [NC]
RewriteRule ^(.*) https://example.com.tw/$1 [R=301,L]

上述的問題可以解決
訪客今天輸入http://www.example.com.tw會自動轉跳到https://example.com.tw
訪客今天輸入http://example.com.tw也會自動轉跳到https://example.com.tw
但如果訪客今天輸入https://www.example.com.tw
會停留在https://www.example.com.tw而不會自動轉跳到https://example.com.tw

3.
爬了不少文似乎沒有解決辦法
最後找到了

var targetProtocol = "https:";
if (.protocol != targetProtocol)
.href = targetProtocol +
.href.substring(.protocol.length);

使用這個+上
.htaccsee中設置
RewriteCond %{HTTP_HOST} ^(www\.example\.com\.tw)(:80)? [NC]
RewriteRule ^(.*) http://example.com.tw/$1 [R=301,L]
結果:
訪客今天輸入
http://www.example.com.tw
http://example.com.tw
https://www.example.com.tw
都會自動轉到https://example.com.tw

一般網站是可行了,因為一般網站可以自主性新增javascript
但今天運行的是Discuz這類的論壇
javascript的方法變成不可行(找了老半天不知道要加在哪裡)
變成只剩下設置.htaccsee的方法

4.
請益使用設置.htaccsee的方法
要透過什麼方式才能夠讓
http://www.example.com.tw
http://example.com.tw
https://www.example.com.tw
訪客輸入這3種格式的網址均可以自動轉跳至https://example.com.tw呢?
2017-02-12 0:34 發佈

jack265s wrote:
1.
當我們在.htaccsee...(恕刪)



你要的需求如下:
要透過什麼方式才能夠讓
http://www.example.com.tw
http://example.com.tw
https://www.example.com.tw
訪客輸入這3種格式的網址均可以自動轉跳至https://example.com.tw呢?


# 針對主機做判斷
RewriteCond %{HTTP_HOST} !^example.com.tw$ [NC]
RewriteRule ^(.*)$ https://example.com.tw/$1 [R=301,L]

# 針對協定做判斷
RewriteCond %{HTTPS} !on
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,QSA]

lun0147 wrote:
你要的需求如下:要...(恕刪)
用上述的辦法
當訪客輸入https://www.example.com.tw
依然會停在https://www.example.com.tw
而不會自動轉跳到https://example.com.tw
先貼上你目前的htaccess來看看,
以及apache 的站台設定 

lun0147 wrote:
先貼上你目前的htaccess...(恕刪)
您好,首先感謝您關注問題。

.htaccess目前是這樣的,

<Files ~ "^\.(htaccess|htpasswd)$">
deny from all
</Files>
Options -Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.example\.com\.tw)(:80)? [NC]
RewriteRule ^(.*) http://example.com.tw/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,QSA]
DirectoryIndex index.php
order deny,allow

apache站台設定部分,
因為是使用免費的虛擬主機關係,所以本身並沒有再去設定這部分。...
jack265s wrote:
您好,首先感謝您關注...(恕刪)



請將這行
RewriteCond %{HTTP_HOST} ^(www\.example\.com\.tw)(:80)? [NC]

主要是
RewriteCond %{HTTP_HOST} !^example.com.tw$ [NC]

你多了 www
這段是說 非 符合網址(example.com.tw) 一律轉址

其實你就照我貼給你的改上去即可。
不用畫蛇添足

將 example.com.tw 取代成你想要的網址即可。


<Files ~ "^\.(htaccess|htpasswd)$">
deny from all
Files>
Options -Indexes
RewriteEngine on

# 針對主機做判斷
RewriteCond %{HTTP_HOST} !^example.com.tw$ [NC]
RewriteRule ^(.*)$ https://example.com.tw/$1 [R=301,L]

# 針對協定做判斷
RewriteCond %{HTTPS} !on
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,QSA]

DirectoryIndex index.php
order deny,allow

jack265s wrote:
1.當我們在.htaccsee...(恕刪)


我的做法是如下

RewriteEngine On
RewriteBase /
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]


不但,可以直接傳回 http 200 OK 訊息
內文搜尋
X
評分
評分
複製連結
Mobile01提醒您
您目前瀏覽的是行動版網頁
是否切換到電腦版網頁呢?