.htaccess - 使用 .htaccess 将 http 重定向到带有 www 的 https
问题描述
如何将 HTTP 重定向到 https 包括 WWW 使用.htaccess
?
例子 :
我正在努力
RewriteEngine On
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"' [OR]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
解决方案
您可以将以下代码放入您的.htaccess
文件中
RewriteEngine On
# ensure www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
该.htaccess
文件会将http://example.com/重定向到https://example.com/。
代码说明:
[NC]
匹配 URL 的大小写版本- (
X-Forwarded-Proto
XFP) 标头是用于识别协议的事实上的标准标头
推荐阅读
- sql - 将 SQL 查询格式化为 XML 文件
- php - Silex:将字符串定义为路由参数的类型提示,它显示字符串不存在
- python - 如何指向 django 将模板放在首页
- python - 无法从网页中获取文本
- javascript - addEvent 类型不匹配 IE8
- c# - C# 等效于 OpenSSL 签名
- python - 如果满足条件,则在 excel 中选择特定行条目并打印整行
- mysql - 如何用 Laravel 锁定表?
- cordova - 无法从我的 Ionic 3.0 Windows 模拟器在我的服务器上进行 REST API 调用
- java - Spring Data - Redis 存储库不使用 pageRequest 排序