Apache-2.2

將 HTTPS 重定向到 HTTP,特定 URL 除外

  • September 23, 2011

我們希望將所有 HTTPS 流量重定向到 HTTP,但特定 URL 是 /user/login

到目前為止,我們得到了:

RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^user/login(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]

但它會導致重定向循環,當它重定向回 HTTP

我相信這應該可以解決問題:

RewriteEngine On
RewriteBase /

# Turn SSL on for /user/login
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/user/login
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

# Turn SSL off everything but /user/login
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/user/login
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]

以上將執行以下操作:

1. User types: https://yourdomain.com/user/login - no redirect
2. User types: http://yourdomain.com/user/login -> redirect to: https://yourdomain.com/user/login
3. User types: https://yourdomain.com/somerandomfile.php -> redirect to: http://yourdomain.com/somerandomfile.php
4. User types: http://yourdomain.com/somerandomfile.php - no redirect

引用自:https://serverfault.com/questions/311699