【问题标题】:.htaccess: Redirect main domain to https://www, subdomain to https:// (without www).htaccess:将主域重定向到 https://www,子域重定向到 https://(不带 www)
【发布时间】:2018-06-02 19:46:09
【问题描述】:

我知道这里有无数提供的.htaccess-solutions,但没有一个重定向到httpswww 仅用于主域,但https 没有www 仅用于子域.基于this问题和php的解决方案,现在我想修改我的.htaccess文件以满足以下需求。

主域:

  • http 重定向到https
  • non-www 重定向到www
  • (对于主域:www需要并且应该出现)

子域:

  • http 重定向到https
  • www 重定向到non-www
  • (对于子域:www 需要并且应该出现)

我目前的做法是这样的:

RewriteEngine on

# Ensure https for all domains (main- and subdomains):
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# Redirect non-www to www only for main domain, but not for subdomains:
RewriteCond %{HTTP_HOST} ^(domain\.com)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L]

# Redirect www to non-www only for subdomains:
RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://subdomain.domain.com%{REQUEST_URI} [R=301,L,NE]

  • 我认为子域有错误?
  • 我不明白第一个命令中的RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} 部分:当访问者进入没有https 的子域时,此命令不会将访问者发送到主域吗?或者{HTTP_HOST} 是否已经包含可能的子域?

评论:子域的内容与主域完全不同(可能是 private.website.comcloud.website.com 例如)。它不仅仅是一种不同的语言。因此,通过浏览器源或 CMS 创建内部转发似乎没有用。只能通过.htaccess-entry 执行。

【问题讨论】:

    标签: .htaccess redirect https dns subdomain


    【解决方案1】:

    子域部分的规则是重复的,你应该修改成这样:

    RewriteCond %{HTTP_HOST} ^www\.subdomain\.domain\.com$ [NC]
    RewriteRule ^ https://subdomain.domain.com%{REQUEST_URI} [R=301,L,NE]
    

    对于这些规则:

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

    第 1 行:如果 https 关闭

    第 2 行:对于任何模式,将用户永久重定向到 https://%{HTTP_HOST}%{REQUEST_URI}%{HTTP_HOST} 是从HTTP request header 获得的,当用户发出请求domain.com 时,%{HTTP_HOST}domain.com,而发出此请求subdomain.domain.com,则%{HTTP_HOST}subdomain.domain.com

    【讨论】:

      猜你喜欢
      • 2018-06-01
      • 1970-01-01
      • 2017-04-22
      • 2015-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-21
      • 1970-01-01
      相关资源
      最近更新 更多