简体   繁体   English

htaccess 将域重定向到 https,带参数的子域到 http

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

I'm trying to do that:我正在尝试这样做:

Force the https for my main domain.为我的主域强制使用 https。

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

this is htaccess coding for domain这是域的 htaccess 编码

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

this is working fine这工作正常

But not for subdomains但不适用于子域

I want result as我想要结果

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

And always removing the www and https to http.并始终将 www 和 https 删除为 http。

but no idea how to write htaccess code for subdomain但不知道如何为子域编写 htaccess 代码

This will first check if you need to redirect to a subdomain based on the username parameter in the query string if requesting index.php.如果请求 index.php,这将首先检查您是否需要根据查询字符串中的用户名参数重定向到子域。 Then when rewritting to that subdomain, it will copy the entire querystring along with, but omitting the username parameter from it while preserving all other parameters.然后当重写到该子域时,它会复制整个查询字符串,但在保留所有其他参数的同时省略用户名参数。

The second part will check for your primary domain with or without www, then enforce https on it, if we didn't already decide to redirect to a subdomain.第二部分将检查带或不带 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