非www到https上的www

时间:2018-12-14 16:16:45

标签: .htaccess ssl

我在将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吗?

1 个答案:

答案 0 :(得分:0)

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

让我们分解一下您拥有的东西:

# 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分钟进行搜索,您可能会发现大约一百万页关于如何执行此操作;但是,如果您不知道他们的操作,那么您将如何知道该信任哪一个?)