强制https在整个站点上但不在一个文件夹上

时间:2015-09-02 19:16:35

标签: apache .htaccess mod-rewrite https

有人能告诉我如何在我的整个网站上强制使用https,但不能在单个文件夹或网址上强制使用https。 目前我有这段代码:

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^thatmysite\.com [NC]
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://thatmysite.com/$1 [R,L]

但是如果我在root htaccess中添加第二个代码来从/thatsite.com/printing文件夹中删除https,我会得到一个重定向循环,因为我强制将代码http转换为https而不是https转换为http ...

RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{REQUEST_URI} ^\/(printing)
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L,R=301]

RewriteCond %{HTTP:X-Forwarded-SSL} =on
RewriteCond %{REQUEST_URI} !^\/(printing)
RewriteRule (.*) http://%{HTTP_HOST}/$1 [L,R=301]

你知道解决这个问题的方法吗?我一直在互联网上寻找并找不到一个好的答案。

2 个答案:

答案 0 :(得分:1)

在.htaccess:

之上尝试这两条规则
RewriteEngine On

RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !/printing [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} /printing [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

答案 1 :(得分:0)

我通过将此代码放在.htaccess文件中来解决此问题,该文件是托管http网站的目录的根文件夹(例如public_html)

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/TheFolderYouCanAccessWithoutHttp/
RewriteRule (.*) https://yourdomain.xyz/$1 [R=301,L]