如何使用.htaccess
将http重定向到包含WWW的https?
示例:
我正在尝试
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]
答案 0 :(得分:3)
您可以将以下代码放入.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/。
代码说明 n:
[NC]
匹配URL的大写和小写版本X-Forwarded-Proto
(XFP)标头是用于识别协议的事实上的标准标头答案 1 :(得分:0)
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]
尝试一下。希望对您有帮助。
答案 2 :(得分:0)
尝试一下
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
答案 3 :(得分:0)
RewriteCond %{HTTPS} off
RewriteCond %{HTTPS_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
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;
}
您可以替换ssl on;带监听443 SSL的指令;作为nginx documentation的推荐。