htaccess 強制 https 和 www 到非 www,但子域僅 https (htaccess force https & www to non-www, but subdomain only https)


問題描述

htaccess 強制 https 和 www 到非 www,但子域僅 https (htaccess force https & www to non‑www, but subdomain only https)

我有兩個網站

  1. yourdomain.com
  2. reseller.yourdomain.com

現在我想強制兩個域通過 apache htaccess 重定向使用 https。除此之外,主域應始終使用 www 而不是非 www。但是子域 reseller.yourdomain.com 當然不應該使用 www。

重定向應該是什麼樣子?我已經嘗試了很多類似

RewriteEngine on
RewriteCond %{HTTPS} !^on

RewriteCond %{HTTP_HOST} !^reseller\.yourdomain\.com [NC]

RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

這不是100%,主域正確重定向到www和ssl,subomdain的ssl重定向僅適用於reseller.yourdomain.com,不適用於sub ‑ 像 reseller.yourdomain.com/single‑page 這樣的網站。

謝謝!


參考解法

方法 1:

Try it like this,

RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^yourdomain.com
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301]

RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(.+)\.yourdomain.com$
RewriteRule ^ https://%1.yourdomain.com [R=301,L]

(by user6573193Abhishek Gurjar)

參考文件

  1. htaccess force https & www to non‑www, but subdomain only https (CC BY‑SA 2.5/3.0/4.0)

#.htaccess #apache #SSL #redirect






相關問題

使用 htaccess 從基本 URL 中刪除變量 (Remove variable from base URL with htaccess)

多個相同的 url htaccess 重定向 (Multiple same url htaccess redirect)

使用 mod_rewrite 更改 URL 中的單詞的問題 (Issue changing a word in a URL using mod_rewrite)

無法在 XAMPP 中進行 301.htaccess 重定向 (Cannot 301.htaccess redirect in XAMPP)

Cách ẩn mọi thứ sau tên trang web của bạn bằng .htaccess (How to hide everything after your webpage name with .htaccess)

htaccess 顯示內部服務器錯誤 (htaccess show internal server error)

mod-rewrite 結合了兩種不同的重寫規則 (mod-rewrite combine two different rewrite rules)

安全的 PHP 文件上傳 (Secure PHP file uploading)

用 .htaccess 重寫基於日期的 URL:s - 解決方案? (Rewriting date-based URL:s with .htaccess - solution?)

htaccess 強制 https 和 www 到非 www,但子域僅 https (htaccess force https & www to non-www, but subdomain only https)

重定向太多。設置 htaccess 以重定向 (too many redirects. setting up htaccess to redirect)

.htaccess https、www 和子域靜默重寫 (.htaccess https, www and subdomain silent rewrite)







留言討論