[英]Redirect http to https with www using .htaccess
How can I redirect HTTP to https include WWW using .htaccess
?如何使用
.htaccess
将 HTTP 重定向到 https 包括 WWW?
Example :例子 :
- redirect http://example.com to https://www.example.com
将http://example.com重定向到https://www.example.com
- redirect http://www.example.com to https://www.example.com
将http://www.example.com重定向到https://www.example.com
- redirect https://example.com to https://www.example.com
将https://example.com重定向到https://www.example.com
I'm trying我想
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]
You can put the following code inside your .htaccess
file您可以将以下代码放入
.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]
This .htaccess
file will redirect http://example.com/ to https://example.com/ .这个
.htaccess
文件会将http://example.com/重定向到https://example.com/ 。
Code Explanatio n:代码说明:
[NC]
matches both upper and lower case versions of the URL [NC]
匹配 URL 的大小写版本X-Forwarded-Proto
(XFP) header is a de-facto standard header for identifying the protocol X-Forwarded-Proto
(XFP) 标头是用于识别协议的事实上的标准标头RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80 //If you want the condition to match port
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
Try in this.试试这个。 Hope this may help you.
希望这可以帮助你。
Try this尝试这个
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTPS_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
NGINX NGINX
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /srv/www/example.com/keys/ssl.crt;
ssl_certificate_key /srv/www/example.com/keys/www.example.com.key;
return 301 https://www.example.com$request_uri;
}
You can replace ssl on;你可以替换ssl; directive with listen 443 ssl;
指令与听 443 ssl; as recommendation from nginx documentation .
作为nginx 文档的推荐。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.