apache 使用 htaccess 添加带有 https 支持的 www

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2097542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 18:39:05  来源:igfitidea点击:

use htaccess to add www with https support

apachehttp.htaccessmod-rewritehttps

提问by quinn

There are a lot of code examples for using .htaccess to add www to a url but they are usually confined to using http. does anybody have an example of how to get it to work in both cases?

有很多使用 .htaccess 将 www 添加到 url 的代码示例,但它们通常仅限于使用 http。有没有人有如何让它在这两种情况下工作的例子?

回答by Gumbo

Sure:

当然:

RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

The second condition checks if the HTTPSenvironment variable (either onor off) is set to onand captures the appended sthat is then available with %1. If it doesn't match, %1is just an empty string.

第二个条件检查HTTPS环境变量(onoff)是否设置为on并捕获s随后可用的附加%1。如果不匹配,%1则只是一个空字符串。

回答by quinn

use this code in .htaccess and it will add both https and www

在 .htaccess 中使用此代码,它将同时添加 https 和 www

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteCond %{HTTPS}s ^on(s)|
    RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]