使用.htaccess重定向http://,http://www.和https://www.到https:// [英] Using .htaccess to redirect http://, http://www., and https://www. to https://

查看:51
本文介绍了使用.htaccess重定向http://,http://www.和https://www.到https://的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎已经有人问过这个问题的各种排列方式,但似乎所有答案都有特定的特质,不适用于我的情况,或者就是行不通.

It looks like various permutations of this question have been asked before, but it looks like all of the answers have specific idiosyncrasies that don't apply to my situation, or just don't work.

所以,这就是我的追求...

So, here's what I'm after...

我想要这三个URL:

  1. http://example.com
  2. http://www.example.com
  3. https://www.example.com
  1. http://example.com
  2. http://www.example.com
  3. https://www.example.com

...全部重定向到:

... To all redirect to:

https://example.com

从另一个答案中,我尝试了这个;

From another answer, I tried this;

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

    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>

但是,这不会从 https://www.example.com 重定向到 https://example.com

But, that doesn't redirect from https://www.example.com to https://example.com

因此,结果是证书警告(因为我的证书仅对example.com有效,对www.example.com无效).

So, the result is a certificate warning (since my certificate is only valid for example.com, not www.example.com).

我也尝试过此操作(来自 https://html5boilerplate.com/)...

I also tried this (from https://html5boilerplate.com/)...

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

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

...但这也不起作用:

... But that didn't work either:

  • http://www.example.com was redirected to https://www.example.com
  • https://www.example.com was not redirected

预先感谢您的帮助.

推荐答案

尝试:

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^ https://example.com%{REQUEST_URI} [L,R=301]

这使用OR条件标志,以便在主机不是 https (如果主机以"www"开头)时发生重定向.

This uses the OR condition flag such that the redirect happens when either it isn't https or if the host starts with "www".

这里还有其他值得注意的地方:

Something else that's worth noting here:

因为我的证书仅对example.com有效,对www.example.com无效

since my certificate is only valid for example.com, not www.example.com

SSL握手发生在发送HTTP请求之前.这意味着当您请求https://www.example.com/时,mod_rewrite或htaccess甚至都不是等式的一部分,浏览器将看到cert并立即将其标记为安全异常.除了购买新证书,您无能为力.

The SSL handshake happens before the HTTP request is sent. That means mod_rewrite or htaccess isn't even part of the equation when you request https://www.example.com/, the browser will see the cert and immediately flag it as a security exception. Nothing you can do about that except buy a new cert.

这篇关于使用.htaccess重定向http://,http://www.和https://www.到https://的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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