如何将任何输入重定向到 https 和 www?

How to redirect any input to both https and www?

我需要 example.com 重定向到 https://www.example.com

我的.htaccess文件在根目录下,配置如下:

RewriteEngine On
DirectoryIndex html/home5.html
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

使用此配置,www.example.comhttps://example.comhttp://example.com 都正确重定向到 https://www.example.comwww.example.com/other.html 等分别工作)。

但是 example.com 被重定向到 https://www.example.com/http://www.example.com/ 并且显然会生成“在此服务器上找不到请求的 URL”。 为什么会这样?我需要更改什么才能使其正常工作?

我也不明白为什么我当前的配置 https://example.com www 可以正确添加。

有一个工作良好的 .htacces,我正在使用它来解决同样的问题,只需 2 个步骤:

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

首先,如果请求的 url 关闭了 HTTPS,则重定向到 HTTPS 并在(保存在 $1 中)之后添加 url 请求查询。

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%1/ [L]

然后,www 的特例。子域和 https on,如果在请求中匹配“www”,则重定向到 https on !

的同一页面

编辑:事实上你甚至不需要第二个条件

重定向到 https 和 non-www

要将所有请求重定向到 https 和 non-www,请使用以下代码代替之前的代码:

规范HTTPS/non-WWW

<IfModule mod_rewrite.c>
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
    RewriteRule (.*) https://example.com/ [L,R=301]
</IfModule>

和以前一样,将此代码放在您网站的根目录 .htaccess 中。这是它正在做的事情:

Checks if mod_rewrite is available
Checks if HTTPS is off, or if the request includes www
If either condition matches, the request qualifies and is redirected to the https/non-www address

当放置在根 .htaccess 中时,此技术涵盖所有请求,为您的站点提供完整的 https/non-www 规范化。请记住将 example.com 的两个实例替换为您自己的域名。

注意:如果您的网站因为 index.php 附加到请求的 URL 页面而出现重复页面,请查看 WP-Mix.com 上的 post 解释如何从 URL 中删除 www 和 index.php。 重定向到 https 和 www

以下 .htaccess 技术将符合条件的请求重定向到网页的 https 和 www 版本。添加到您网站的根 .htaccess 文件:

规范https/www

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

此代码执行以下操作:

The first two lines conditionally redirect to https. If the HTTPS variable is set to off, then the request is redirected to https (see notes below if using a proxy).
The second two lines redirect to www. If the request/host does not begin with www., the request is redirected to www.

当放置在根 .htaccess 中时,此技术涵盖所有请求,为您的站点提供完整的 https/www 规范化。此代码无需编辑;完全是plug-n-play。 使用代理时的注意事项

如此处解释:

“当在某些形式的代理后面时,客户端通过 HTTPS 连接到代理、负载平衡器、乘客应用程序等,%{HTTPS} 变量可能永远不会打开并导致重写循环。这是因为即使客户端和 proxy/load 平衡器使用 HTTPS,您的应用程序实际上正在接收纯 HTTP 流量。在这些情况下,请检查 X-Forwarded-Proto header 而不是 %{HTTPS} 变量."

因此,如果您使用某种代理服务或类似服务,请在上面的代码中添加以下行:

RewriteCond %{HTTP:X-Forwarded-Proto} !https

因此,如果使用代理服务器,最终结果如下所示:

规范 https/www(使用代理时)

<IfModule mod_rewrite.c>
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

reference