2010-07-22 67 views
16

我有一个客户端项目,我需要为某个文件夹强制使用HTTPS,并强制所有其他文件夹使用HTTP。我可以成功地为我想要的文件夹强制执行HTTPS,但随后通过HTTPS返回到网站的其余部分。我想有一个强制请求任何'不'在安全文件夹强制回到HTTP的规则。这是我到目前为止:强制某些URL上的HTTPS并强制所有其他的HTTP

RewriteEngine On 
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC] 

RewriteCond %{HTTPS} !=on 
RewriteRule ^(my) https://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /index.php/$1 

'my'是我需要强制使用HTTPS的文件夹的名称。

任何想法?

更新:我也尝试:

RewriteEngine On 
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC] 

# Force HTTPS for /my 
RewriteCond %{HTTPS} !=on 
RewriteRule ^(my) https://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L] 

# Force HTTP for anything which isn't /my 
RewriteCond %{HTTPS} =on 
RewriteRule !^my http://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L] 

# Remove index.php from URLs 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /index.php/$1 

但不是我正在通过HTTPS迫于/请求他们现在只是解析为http://www.example.com/index.php/my

:?

回答

16

啊,当然。问题在于,您的重写规则集将在初始重定向后转换为index.php后进行重新处理。使用您目前拥有的内容,您需要额外调整重定向,以确保在重写为/index.php/my后不会被应用。

类似下面应该做的:

RewriteEngine On 
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC] 

# Force HTTPS for /my 
RewriteCond %{HTTPS} !=on 
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/my [NC] 
RewriteRule ^(my) https://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L] 

# Force HTTP for anything which isn't /my 
RewriteCond %{HTTPS} =on 
RewriteCond %{THE_REQUEST} !^[A-Z]+\s/my [NC] 
RewriteRule !^my http://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L] 

# Remove index.php from URLs 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /index.php/$1 
+0

谢谢,节省了我很多时间! FWIW,在Wordpress中,基本的Wordpress htaccess IfModule已经从URLs中删除了index.php。 – Jason 2016-01-19 20:47:39

0

只是反转条件:

RewriteCond %{HTTPS} =on 
RewriteRule !^my http://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L] 
+0

喜雅,感谢您的快速回复。我尝试过,但我不确定将它放在现有规则的流程中,任何指针?如果我将它放在现有的“强制HTTPS”规则之上,它会阻止“强制HTTPS”的运行,并阻止重写index.php的最终规则执行。 :? – 2010-07-22 08:44:03

+0

@Nathan Pitman:不,由于他们有不同的匹配条件,他们不应该互动。 – Gumbo 2010-07-22 08:56:22

+0

好吧,我尝试了你的建议(请参阅我原来的帖子的更新),但这不起作用。我可以看到它'应该',但我认为我可能有错误的顺序规则或某些规则导致重写在其他条件和规则可以评估之前终止...:/ – 2010-07-22 09:29:14

1

这一点是从旧的客户网站工作并能为您的目的是适应性强:

#If https off and in the cart dir 
RewriteCond %{HTTPS} =off [NC] 
RewriteCond %{REQUEST_URI} ^/cart/(.*) [NC] 
RewriteRule ^(.*)$ https://%{HTTP_HOST}/cart/%1 [R=301,L] 

#If https on and not in cart dir  
RewriteCond %{HTTPS} =on 
RewriteCond %{REQUEST_URI} !^/cart [NC] 
#Above line actually used to read RewriteCond %{REQUEST_URI} !^/cart|media|images|thumbs|css|js [NC] 
#to allow js/css/images to be served so there were no mixed ssl messages popping up to visitors 
RewriteCond %{REQUEST_FILENAME} !index\.php$ [NC] 
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L] 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] 

我也许

4

更换车请尝试以下方法,应为您工作:

RewriteEngine On 

RewriteCond %{HTTPS} off 
RewriteCond %{REQUEST_URI} ^/my 
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L] 

RewriteCond %{HTTPS} on 
RewriteCond %{REQUEST_URI} !^/my 
RewriteRule ^(.*)$ http://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]