簡體   English   中英

使用 .htaccess 為子域強制使用 SSL HTTPS

[英]Force SSL HTTPS for subdomain with .htaccess

我有這個代碼通常用於在網站上強制使用 SSL:

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

我從這里得到的

我希望它僅適用於特定的子域

例如,如果您要訪問http://sub.domain.org它應該重定向到https://sub.domain.org

我有一種預感,我需要添加另一個RewriteCond ,它基本上表示被命中的域是http://sub.domain.org ,但我不知道如何編寫此代碼。

我一直在研究其他 stackoverflow 問題,例如:

域強制使用 HTTPS 和 WWW,子域僅使用 HTTPS HTACESS

使用 .htaccess 和 mod_rewrite 強制 SSL/https

和 Apache 文檔: https : //httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecond

我有過這些嘗試:

RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^(reports\.)?uksacb\.org$ [NC]
RewriteRule ^.*$ https://%1%{REQUEST_URI} [R,L]

有點顛倒了這個問題/答案中的答案

RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} =reports.uksacb.org
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

但是我仍然無法理解如何正確地做到這一點。

如果有人能向我解釋我做錯了什么,我需要做些什么來使它正確,以及如何調試他們的重寫條件和規則以確保它正常工作,我將非常感激。 有時我認為我的瀏覽器正在緩存我編寫的規則,而我的不同嘗試沒有任何影響!

我的整個.htaccess看起來像這樣:

選項 - 多視圖

RewriteEngine On

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Force SSL
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

為了避免使用域名或子域名,我曾經使用以下代碼:

  # Redirect HTTP to HTTPS
  RewriteCond %{HTTPS} off
  RewriteCond %{HTTP:X-Forwarded-Proto} !https
  RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

我更喜歡沒有 WWW。

%{HTTPS}如果請求是通過 SSL 提供的,則設置為 on。

%{HTTP_HOST}協議(http:// 或 https://)和請求(/example-file.txt)之間的所有內容。

%{REQUEST_URI}服務器地址之后的所有內容 - 例如,對http://example.com/my/example-file.txt的請求將此值設置為 my/example-file.txt。

%{HTTP:X-Forwarded-Proto}請求 URL 時使用的協議 — 設置為 http 或 https。

我希望它對任何人都有用。

為了調試,我使用了一個我發現的網站https://htaccess.madewithlove.be/

使用您提供的規則

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

我有一個錯誤

RewriteCond %{HTTPS} !=on

這可能對你有用

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

https://www.sslshopper.com/apache-redirect-http-to-https.html找到了上面的例子

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM