【问题标题】:Force use HTTPS with .COM domain and WWW in .htaccess强制将 HTTPS 与 .COM 域和 .htaccess 中的 WWW 一起使用
【发布时间】:2020-12-16 22:55:23
【问题描述】:

我正在尝试对所有到主 .com 域的流量进行永久重定向 301。 我有 3 个域:

example.com -> Main Domain
example.net
example.org

规则是:

  • 始终使用 https (http -> https)
  • 仅使用 .com 域 (.org|.net|AnyOther -> .com)
  • 强制 www 仅适用于根域 *排除子域(非 www -> wwww)

例子:

http://example.com -> https://www.example.com
http://example.net -> https://www.example.com
http://example.org -> https://www.example.com

https://example.com -> https://www.example.com
https://example.net -> https://www.example.com
https://example.org -> https://www.example.com

http://www.example.com -> https://www.example.com
http://www.example.net -> https://www.example.com
http://www.example.org -> https://www.example.com

https://www.example.com -> https://www.example.com -> OK
https://www.example.net -> https://www.example.com
https://www.example.org -> https://www.example.com

http://example.com/subfolder -> https://www.example.com/subfolder
http://example.net/subfolder -> https://www.example.com/subfolder
http://example.org/subfolder -> https://www.example.com/subfolder

https://example.com/subfolder -> https://www.example.com/subfolder
https://example.net/subfolder -> https://www.example.com/subfolder
https://example.org/subfolder -> https://www.example.com/subfolder

http://www.example.com/subfolder -> https://www.example.com/subfolder
http://www.example.net/subfolder -> https://www.example.com/subfolder
http://www.example.org/subfolder -> https://www.example.com/subfolder

https://www.example.com/subfolder -> https://www.example.com/subfolder -> OK
https://www.example.net/subfolder -> https://www.example.com/subfolder
https://www.example.org/subfolder -> https://www.example.com/subfolder

http://client1.example.com -> https://client1.example.com
http://client1.example.net -> https://client1.example.com
http://client1.example.org -> https://client1.example.com

https://client1.example.com -> https://client1.example.com -> OK
https://client1.example.net -> https://client1.example.com
https://client1.example.org -> https://client1.example.com

http://client1.example.com/subfolder -> https://client1.example.com/subfolder
http://client1.example.net/subfolder -> https://client1.example.com/subfolder
http://client1.example.org/subfolder -> https://client1.example.com/subfolder

https://client1.example.com/subfolder -> https://client1.example.com/subfolder -> OK
https://client1.example.net/subfolder -> https://client1.example.com/subfolder
https://client1.example.org/subfolder -> https://client1.example.com/subfolder

更新

通常我使用我在 htaccess 中制作的这段代码,但问题是它会将 www 添加到子域。

# Force www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

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

# Force .com
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]

任何帮助将不胜感激

【问题讨论】:

  • 请添加您自己当前的尝试来实现这一点。在 SO 上,仅此一项就有数百个答案。因此,您已经拥有所需的所有信息。如果这没有帮助,令人惊讶的是,那么您需要告诉我们您到底尝试了什么以及哪些细节没有按预期工作。没有它,我们几乎无法帮助解决这个问题。 请注意,我们不是免费的编码服务,因此我们不是来为您工作的。实际上,您应该自己这样做。我们随时为具体问题提供帮助和解答。
  • 你好@arkascha 对不起,我没有添加代码是我的错。请查看我对我目前尝试过的代码的更新问候

标签: apache .htaccess http redirect mod-rewrite


【解决方案1】:

终于经过不断的搜索和调查,我设法修改了代码以使其正常工作,希望对其他人有所帮助。

工作.htaccess

# Force www
RewriteCond %{HTTP_HOST} ^websuiter\.(net|org|com)
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

# Force .com
RewriteCond %{HTTP_HOST} ^(?:www\.)?+(.+?)\.(?!com$)[^.]+$ [NC]
RewriteRule .* http://%1.com/$0 [R=301,NE,L]

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

【讨论】:

  • 太好了,您找到了解决方案。对第一条规则的一个明显改进是在此处立即设置.com TLD,这样可以节省第二条规则的第二次立即重定向。但这只是一个细节。
猜你喜欢
  • 2014-01-20
  • 2016-06-15
  • 2013-05-31
  • 1970-01-01
  • 2016-04-05
  • 1970-01-01
  • 2015-07-18
  • 2019-09-08
  • 2012-07-30
相关资源
最近更新 更多