【问题标题】:non-www to www on httpshttps 上的非 www 到 www
【发布时间】:2018-12-15 01:09:21
【问题描述】:

我在将 http 重定向到 https 时遇到了一些问题。朋友帮我找到了解决办法,但还是有些麻烦。

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?thevideocards\.com
RewriteRule ^(.*)$ https://www.thevideocards.com/$1 [R,L]

这会将 http 重定向到 https,将 http 重定向到 https://www,但不会将 https://example 重定向到 https://www.example。有人知道应该改变什么来强制所有流量到https://www.example.com 吗?

【问题讨论】:

  • 大多数浏览器默认只删除 www(但它仍然存在)
  • 好吧,如果你去example.com,它会一直留在那里

标签: .htaccess ssl


【解决方案1】:

了解您正在编写的代码的含义很重要 - 否则,谁知道有人在说服您添加什么后门。

让我们分解一下你所拥有的:

# Turn on Apache's "Rewrite Engine"; this is the module that processes the next few lines
RewriteEngine On 

# Condition (for following rule): match only if the connection is on port 80
# Port 80 is (by convention) used for plain-text HTTP traffic
# An alternative to this would be to check for a secure connection directly:
# RewriteCond %{HTTPS} !on
RewriteCond %{SERVER_PORT} 80

# Condition (for following rule): match only if the HTTP Host header matches a regex
# The (www\.)? means that prefix is optional, 
#  so it will match both www.thevideocards.com and thevideocards.com
RewriteCond %{HTTP_HOST} ^(www\.)?thevideocards\.com

# Rule (if the above two conditions also match):
# ^(.*)$ - Match any URL, and capture as $1
# https://www.thevideocards.com/$1 - Generate the replacement URL based on the captured part
# [R] - Send redirect response to browser, rather than processing internally
# [L] - Don't process any more rewrite rules for this request
RewriteRule ^(.*)$ https://www.thevideocards.com/$1 [R,L]

根据以上内容,您应该能够为您想要的情况创建自己的规则:

  • 条件:主机匹配没有www.
  • 规则:将所有内容重定向到前面带有https://www. 的相同URL

(或者,花 10 分钟搜索,您可能会发现大约一百万页谈论如何做到这一点;但同样,如果您不知道他们做什么,您怎么知道该信任哪一个?)

【讨论】:

  • 你是对的。有很多很多页面解释并举例说明该怎么做。我关注了一些,但遇到了重定向导致 404 的问题,因为地址是example.com/example.com/page.php。我希望我能花 10 分钟来寻找如何避免这种情况。花了几个小时使用不同的代码并调查出了什么问题。当然,部分原因是我不是程序员,所以不知道这意味着什么。我提出的解决方案是唯一有效的,不是完美的,但有效。谢谢你。我会调查你的解释。
  • @MartinP 很多人似乎认为编程很容易。想象一下你的车遇到了问题:你可以买一本手册,盲目地跟着它,然后毁掉你的引擎;您可以花时间了解正在发生的事情,一周又一周地做功课;或者你可以付钱给已经花时间的机械师。这里也是如此:一旦你花了几个小时学习基本原理,你将需要 10 分钟来解决这个特殊问题;但你必须先学习这些原则 - 或者决定你不那么感兴趣,然后付钱给已经有的人。
猜你喜欢
  • 2015-02-13
  • 1970-01-01
  • 2014-12-16
  • 2019-04-19
  • 1970-01-01
  • 2015-03-07
  • 2012-02-14
  • 1970-01-01
  • 2015-04-06
相关资源
最近更新 更多