中国站
帮助中心 > 安全 > SSL安全证书 > 证书安装 > Apache自动跳转到 HTTPS

Apache自动跳转到 HTTPS

网站根目录新建 .htaccess

  1. RewriteEngine On
  2. RewriteCond %{SERVER_PORT} 80
  3. RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]
  4. RewriteEngine On
  5. RewriteCond %{SERVER_PORT} 80
  6. RewriteCond %{HTTP_HOST} ^example.com [NC,OR]
  7. RewriteCond %{HTTP_HOST} ^www.example.com [NC]
  8. RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]

高级用法 (可选)

  1. RewriteEngine on

强制HTTPS

  1. RewriteCond %{HTTPS} !=on [OR]
  2. RewriteCond %{SERVER_PORT} 80

某些页面强制

  1. RewriteCond %{REQUEST_URI} ^something_secure [OR]
  2. RewriteCond %{REQUEST_URI} ^something_else_secure
  3. RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

强制HTTP

  1. RewriteCond %{HTTPS} =on [OR]
  2. RewriteCond %{SERVER_PORT} 443

某些页面强制

  1. RewriteCond %{REQUEST_URI} ^something_public [OR]
  2. RewriteCond %{REQUEST_URI} ^something_else_public
  3. RewriteRule .* http://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]