簡體   English   中英

htaccess 將域重定向到 https,帶參數的子域到 http

[英]htaccess redirect domain to https, subdomain with parameter to http

我正在嘗試這樣做:

為我的主域強制使用 https。

http 或https://www.domain.com -> https://domain.com
http 或https://domain.com -> https://domain.com

這是域的 htaccess 編碼

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

這工作正常

但不適用於子域

我想要結果

https://www.domain.com/index.php?username=abc => http://abc.domain.com
http://www.domain.com/index.php?username=abc => http://abc.domain.com

並始終將 www 和 https 刪除為 http。

但不知道如何為子域編寫 htaccess 代碼

如果請求 index.php,這將首先檢查您是否需要根據查詢字符串中的用戶名參數重定向到子域。 然后當重寫到該子域時,它會復制整個查詢字符串,但在保留所有其他參數的同時省略用戶名參數。

第二部分將檢查帶或不帶 www 的主域,然后在其上強制使用 https,如果我們還沒有決定重定向到子域。

RewriteEngine On

#match either the www subdomain or no subdomain
  RewriteCond %{HTTP_HOST} ^(?:www.)?(?:domain\.com)$
#which request index.php
  RewriteCond %{REQUEST_URI}  ^/index\.php
#and contain a username paramater
  RewriteCond %{QUERY_STRING} ^(.*?)(?:^|&)username=([^&]+)(.*?)$
#then rewrite them to username.domain.com without the username parameter
  RewriteRule ^ http://%2.domain.com%{REQUEST_URI}?%1%3 [R,QSA,L]

#match either the www subdomain or no subdomain
  RewriteCond %{HTTP_HOST} ^(?:www.)?(?:domain\.com)$
#which was requested without https
  RewriteCond %{HTTPS} off
#then redirect to https
  RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

暫無
暫無

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

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