最佳实践:301 将 HTTP 重定向到 HTTPS(标准域) [英] Best Practice: 301 Redirect HTTP to HTTPS (Standard Domain)

查看:32
本文介绍了最佳实践:301 将 HTTP 重定向到 HTTPS(标准域)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找完美的 301 重定向.但是我找到了很多解决方案,不知道什么是最好的.

I have been searching for the perfect 301 redirect. But I am finding so many solutions and do not know what’s best.

这是我想要做的

  • http://domain.tld/ → https://domain.tld/
  • http://www.domain.tld/ → https://domain.tld/
  • https://www.domain.tld/ → https://domain.tld/

最佳实践 .htacess?

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
</IfModule>

这是我的首选代码.至少到现在为止.

This is my preferred code. At least unil now.

替代方法

我还发现了很多其他方法可以从 HTTP 重定向到 HTTPS.例如:

I also found a lot of other ways to redirect from HTTP to HTTPS. For example:

1.

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

缺少一步?这里没有[R=301,L]?

2.

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

不同的顺序通常更好吗?

我应该使用

RewriteRule ^(.*)$

代替

RewriteRule (.*)

?

3.

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} example.com$ [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L,NE]

使用全域名有什么性能优势吗?我真的需要 NE 吗?([R=301,L,NE] vs. [L,R=301])

Does using the full domain name have any performance advantages? Do I really need NE? ([R=301,L,NE] vs. [L,R=301])

所以,我向所有专家提出问题:从 HTTP 重定向到 HTTPS 以及从 重定向到 <代码>HTTPS:// ?

So, my question to all experts: What's the best (performing) way to redirect both from HTTP to HTTPS and from to HTTPS:// ?

推荐答案

从您最喜欢的解决方案开始:

To start with your favorite solution:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
</IfModule>

在处理非 https URL 的部分中,您将重定向到 %{HTTP_HOST}.然后,如果您的主机名以www"开头,则必须进行第二次重定向以将您从 https://www.domain.tldhttps://domain.tld 应该是您的最终目的地.

In the part handling non-https URLs you are redirecting to %{HTTP_HOST}. Then, in case your host name started with "www", a second redirect has to take place to send you from https://www.domain.tld to https://domain.tld which is supposed to be your final destination.

您可以使用

RewriteRule ^(.*)$ https://domain.tld/%{REQUEST_URI} [L,R=301]

直接在第一条规则中.然后,第二条规则仅适用于尝试访问 https://www.domain.tld 的客户端.

directly in the first rule. The second rule would then only apply to clients, who try to access https://www.domain.tld.

替代方案 1. 出于同样的原因(缺少 HTTP_HOST 可能是 www.domain.tld 的情况)并另外因为缺少 而不起作用[L,R=301].这是必要的,因为您不只是在此处重写 URL,就像在其他类型的重写规则中所做的那样.您正在请求客户端更改其请求的类型 - 这就是您向他发送 301 的 HTTP 代码的原因.

Alternative 1. does not work for the same reason (missing the case that HTTP_HOST could be www.domain.tld) and additionally because of the missing [L,R=301]. This is necessary because you do not just rewrite an URL here, like you could do in other types of rewrite rules. You are requesting the client to change the type of it's request - this is why you are sending him a HTTP code of 301.

关于 RewriteRule 本身的匹配部分,您应该保持一致:如果您想捕获 URI 的部分,您将使用带括号的正则表达式.由于您实际上在这里整体使用它,因此可以只使用任何"的替代方法之一,例如 ^ 并稍后使用 %{REQUEST_URI} .如果你使用了一些捕获(即 (some_regex) 你应该在目标中使用 $1 来引用它(或者任何你要引用的东西)在这里.

Concerning the match part of the RewriteRule itself, you should be consistent: if you want to capture parts of the URI you will use a regular expression with parentheses. As you are in fact using it as a whole here it is fine to just use one of the alternatives for "anything", like ^ and use %{REQUEST_URI} later. If you use some capturing (i.e. (some_regex) you should reference it in the target by using $1 (or whatever you are going to reference) here.

在您的第三个选择中,再次缺少 www + https.

In your 3rd alternative, again www + https is missing.

您可以检查 https 是否关闭域名是否在一个规则中包含前导www",但是重写条件与and"隐式连接.

You can check if https is off or if the domain name contains a leading "www" in one rule, however rewrite conditions are implicitly connected with "and".

所以它应该是:

RewriteCond %{HTTPS} off          [OR]
RewriteCond %{HTTP_HOST} ^www.   [NC]
RewriteRule ^ https://domain.tld%{REQUEST_URI} [R=301,L,NE]

NE 对于将诸如 GET 参数之类的东西传递给新的 URI 来说是必要的,请参阅:

The NE is necessary for passing on things like GET-parameters and the like on to the new URI unchanged, see:

http://httpd.apache.org/docs/2.4/rewrite/标志.html

这篇关于最佳实践:301 将 HTTP 重定向到 HTTPS(标准域)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆