2012-04-16 37 views
2

我:.htaccess for SSL?

RewriteCond %{HTTP_HOST} ^MYDOMAIN\.com$ [NC] 
RewriteRule ^(.*)$ http://www.MYDOMAIN.com/$1 [L,R=301] 

RewriteCond %{HTTPS} !=on 
RewriteCond %{REQUEST_URI} ^/(user|admin|cart) 
RewriteRule ^(.*)$ https://www.MYDOMAIN.com/$1 [R=301,L] 

这有效地路由所有/用户/管理员和/车路径使用SSL。我的问题是相反的。我需要一条规则,如果您不在这些页面上,它会将您重定向到HTTP(无SSL)。

我该怎么做?

+0

**大量的**这样的问题。看到这个问题的规则(在实际问题中,不是答案 - 答案是相关的方面):http://stackoverflow.com/questions/10174278/https-and-http-combined-htaccess – LazyOne 2012-04-16 14:44:18

+0

没有'实际上我知道强制使用非SSL的任何价值。你有尝试实现这个目标的原因吗? – Anthony 2012-05-01 01:26:26

回答

0

只需反转条件!

RewriteCond %{HTTPS} =on 
RewriteCond %{REQUEST_URI} !^/(user|admin|cart) 
RewriteRule ^(.*)$ http://www.MYDOMAIN.com/$1 [R=301,L] 
+0

如果我在之前或之后添加它,它是否重要? – coderama 2012-04-16 14:47:50

+0

它不应该,因为在同一个Rewrite运行过程中两个都不可能是真的。 – 2012-04-16 14:55:37

+0

它不工作....嗯...... – coderama 2012-04-16 15:00:29

5

首先清理浏览器缓存并重新启动浏览器。然后用此替换您的.htaccess:

Options +FollowSymLinks -MultiViews 
# Turn mod_rewrite on 
RewriteEngine On 
RewriteBase/

RewriteCond %{HTTP_HOST} ^MYDOMAIN\.com$ [NC] 
RewriteRule^http://www.MYDOMAIN.com%{REQUEST_URI} [L,R] 

RewriteCond %{HTTPS} !=on 
RewriteRule ^(user|admin|cart)(/.*|)$ https://www.MYDOMAIN.com%{REQUEST_URI} [R,L,NC] 

RewriteCond %{HTTPS} =on 
RewriteRule (?!^(user|admin|cart)(/.*|)$)^.*$ http://www.MYDOMAIN.com%{REQUEST_URI} [R,L,NC] 
+0

我意识到我的代码之前有一个小的错字,所以只是做了一个更正。请现在尝试。 – anubhava 2012-04-30 12:45:19

0

这是行不通?我意识到这可能不是,因为SETENV并不总是得到ReWriteRule前处理,但假设,这应该处理所有的场景:

# Set Correct protocol var based on request: 

## Default to http 
SetEnv correct_protocol http 

## Overwrite to https if secure area 
RewriteCond %{REQUEST_URI} ^/(user|admin|cart) [env=correct_protocol:https] 

# Set Current (Actual) Protocol Env Variable: 

## Again, default to http 
SetEnv current_protocol http 

## Overwrite with https if %{HTTPS} = on 
RewriteCond %{HTTPS} = on [env=current_protocol:https] 

# If current <> correct 
RewriteCond %{ENV:current_protocol} != %{ENV:correct_protocol} 

# rewrite URL using correct: 
RewriteRule .* %{ENV:correct_protocol}://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]