Apache使用htaccess文件重定向http强制https访问

How to Redirect HTTP to HTTPS In Apache & Nginx? (.htaccess & .conf)

Apache中做http到https跳转一般有这下面三种情况:

1.整站强制开启ssl,使用https访问

此规则将符合http访问得80端口情况都重定向到https中。

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]

2.根据域名来重定向

条件满足yourdomain.com再重定向

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^yourdomain\.com [NC]
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]

3.根据指定访问目录

如果访问了folder子目录,跳转到https

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{REQUEST_URI} folder 
RewriteRule ^(.*)$ https://www.yourdomain.com/folder/$1 [R,L]

Post Comment