除了使用.htaccess的一个path之外的每个URL强制使用SSL

我试图强制执行每个url除了第一个段/preview之外的SSL。

 http://test.example.com/preview/blah/blah 

应该被规则忽视; 每个其他的URL都应该被强制使用SSL。 我使用的是CentOS 6.4,Apache和CodeIgniter。

我的.htaccess文件:

 RewriteEngine On RewriteBase / RewriteCond %{HTTPS} !=on RewriteCond %{REQUEST_URI} !^/preview/ RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?/$1 [L] 

通常,CI URL在被最后一条规则重写之前看起来像这样:

 http://test.example.com/index.php?/preview/blah/blah 

我试过了:

 RewriteCond %{HTTPS} !=on RewriteCond %{REQUEST_URI} !^/index.php?/preview/ RewriteCond %{REQUEST_URI} !^/preview/ RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 

那也行不通。 我究竟做错了什么?

你几乎已经拥有了。 完整的解决scheme是:

 RewriteEngine On RewriteBase / RewriteCond %{HTTPS} !=on RewriteCond %{REQUEST_URI} !^/preview RewriteCond %{QUERY_STRING} !^/preview RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?/$1 [L] 

它失败的原因是, http://test.domain.com/preview/blah/blah : http://test.domain.com/preview/blah/blah首先得到解决http://test.domain.com/index.php?/preview/blah/blah并立即这个URL再次重写(htaccess循环与新的URL)。

新url( http://test.domain.com/index.php?/preview/blah/blah与您的条件不符,因为“?”之后的部分不被视为REQUEST_URI的一部分,而是QUERY_STRING的一部分。在http://httpd.apache.org/docs/2.4/en/mod/mod_rewrite.html#rewritecond上的REQUEST_URI的描述