【问题标题】:htaccess Redirect Domain and Subdomains to https and including wwwhtaccess 将域和子域重定向到 https,包括 www
【发布时间】:2018-09-11 09:39:55
【问题描述】:

我在使用 https 重定向 url 时遇到问题

  1. www.domain.com 到 domain.com
  2. www.name.domain.com 到 name.domain.com

域可以,但是子域不行。

这是我的 .htaccess 代码:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,NC]
RewriteCond %{HTTP_HOST} ^www.(.+).example.com [NC]
RewriteRule ^(.*)$ http://%1\.example.com/$1 [R=301,NC]

我正在检查这个,但它不起作用:

# redirect www to none-www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [QSA,R=301,L]

# redirect http to https
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [QSA,R=301,L]

【问题讨论】:

  • 如果将RewriteRule ^(.*)$ http://%1\.example.com/$1 [R=301,NC] 更改为RewriteRule ^(.*)$ https://%1.example.com/$1 [R=301,NC] 会发生什么?
  • 不重定向子域。并且域从 www.example.com 重定向到 www.example.com.example.com,其中第二个“example.com”在代码中:RewriteRule ^ (. *) $ http: //% 1 \ .example .com / $ 1 [R = 301,NC]

标签: apache .htaccess redirect mod-rewrite https


【解决方案1】:

你可以使用它:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,NC]
RewriteCond %{HTTP_HOST} ^www.(.+).example.com [NC]
RewriteRule ^(.*)$ https://%1.example.com/$1 [R=301,NC]

我测试了它,它工作正常,但它取决于你对子域的设置,如果你的子域指向主目录,你需要将你的 htaccess 文件放在主目录中,但是如果你的子域指向另一个目录,你必须将您的 htaccess 文件放在所有目录中,您可以在主机面板中检查它

【讨论】:

  • 你是对的 Danial,对于子域的工作,它是正确的。非常感谢。
猜你喜欢
  • 2014-11-12
  • 1970-01-01
  • 2018-06-02
  • 1970-01-01
  • 2015-05-19
  • 2019-02-21
  • 1970-01-01
  • 2017-04-22
  • 1970-01-01
相关资源
最近更新 更多