【问题标题】:Selective SSL with .htaccess force http:// or https://带有 .htaccess 的选择性 SSL 强制 http:// 或 https://
【发布时间】:2011-03-02 08:44:08
【问题描述】:

出于性能原因,我试图在哪些页面/请求使用SSL 时更具选择性。我想使用htaccess 重定向到https:// 仅用于所需的页面,并重定向回http:// 用于其他所有内容。这就是我所拥有的:

RewriteEngine On

# force https
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)/(abc|xyz)(.*)$ https://%{HTTP_HOST}/$1/$2 [R=301,NC,L]

RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{HTTP_REFERER} !^https(.*)/(abc|xyz)(.*)$ [NC]
RewriteCond %{REQUEST_URI} !^(.*)/(abc|xyz)(.*)$ [NC]
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ initialize.php [QSA,L]

安全请求应为abcxyz。问题是如果请求http:// example.com/abc,它会重定向到http://example.com/initialize.php。非安全页面按预期工作。

【问题讨论】:

    标签: performance apache http .htaccess ssl


    【解决方案1】:

    由于这是一个几年前的问题,我很确定它已经解决了,但对于可能遇到这个问题的人,我认为这样的事情可能会奏效。很难知道上面的abcxyz 是什么,但我会假设它们是URI 路径的一部分。

    # Forward some URI's to HTTPS.
    RewriteCond %{HTTPS} =off
    RewriteCond $1 (abc|xyz) [NC]
    RewriteRule ^(.+)$ https://%{HTTP_HOST}%{REQUEST_URI} [QSA,S=1,L]
    
    # Forward rest of the request to HTTP
    RewriteCond %{HTTPS} =on
    RewriteCond $1 !(abc|xyz) [NC]
    RewriteRule ^(.+)$ http://%{HTTP_HOST}%{REQUEST_URI} [QSA,L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ initialize.php [QSA,L]
    

    此外,解决此问题的技巧是启用mod_rewrite logging

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-02
      • 2018-11-28
      • 1970-01-01
      • 2017-08-22
      • 1970-01-01
      • 2011-05-22
      • 2015-07-05
      相关资源
      最近更新 更多