简体   繁体   English

使用 .htaccess 将 http 重定向到带有 www 的 https

[英]Redirect http to https with www using .htaccess

How can I redirect HTTP to https include WWW using .htaccess ?如何使用.htaccess将 HTTP 重定向到 https 包括 WWW?

Example :例子 :

  1. redirect http://example.com to https://www.example.comhttp://example.com重定向到https://www.example.com
  2. redirect http://www.example.com to https://www.example.comhttp://www.example.com重定向到https://www.example.com
  3. redirect https://example.com to https://www.example.comhttps://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 的大小写版本
  • The 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]

APACHE APACHE

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.

相关问题 使用htaccess将http:// www,http://和https:// www重定向到https: - Using htaccess to redirect http://www, http:// and https://www to https: 如何使用.htaccess将http:// http:// www和https:// www重定向到https:// - How to redirect http:// http://www and https://www to https:// using .htaccess 使用.htaccess使用https将www和http重定向到非www - Redirect www and http to non-www with https using .htaccess 如何使用.htaccess将http重定向到https用于www和非www用于目录 - How to redirect http to https for www and non www for a directory using .htaccess .htaccess http www或非www重定向到https - .htaccess http www or non www redirect to https 使用 htaccess 将 http:// 和 http://www 重写/重定向到 https:// - rewrite/redirect http:// and http://www to https:// using htaccess htaccess重定向:WWW + HTTPS:// WWW + HTTP:// ---> HTTPS:// non-www - Htaccess Redirect: WWW + HTTPS://WWW + HTTP:// ---> HTTPS://non-www Htaccess将http:// www…重定向到https:// www…,但不重定向http:// www - Htaccess redirects http://www… to https://www…, but does not redirect http://www 如何使用 .htaccess 将任何 http 重定向到 https 并删除 aws 中的 www - How to redirect any http to https and remove www in aws using .htaccess 使用.htaccess重定向http://,http:// www。和https:// www。 到https:// - Using .htaccess to redirect http://, http://www., and https://www. to https://
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM