Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache 对某些URL强制使用HTTPS,对所有其他URL强制使用HTTP_Apache_Http_Mod Rewrite_Https - Fatal编程技术网

Apache 对某些URL强制使用HTTPS,对所有其他URL强制使用HTTP

Apache 对某些URL强制使用HTTPS,对所有其他URL强制使用HTTP,apache,http,mod-rewrite,https,Apache,Http,Mod Rewrite,Https,我有一个客户端项目,我需要对某个文件夹强制使用HTTPS,对所有其他文件夹强制使用HTTP。我可以成功地为我想要的文件夹强制使用HTTPS,但是所有指向站点其余部分的链接最终都是通过HTTPS实现的。我希望有一个规则,强制安全文件夹中任何“非”内容的请求返回HTTP。以下是我目前掌握的情况: RewriteEngine On RewriteCond $1 !\.(gif|jpe?g|png)$ [NC] RewriteCond %{HTTPS} !=on RewriteRule ^(my) h

我有一个客户端项目,我需要对某个文件夹强制使用HTTPS,对所有其他文件夹强制使用HTTP。我可以成功地为我想要的文件夹强制使用HTTPS,但是所有指向站点其余部分的链接最终都是通过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强制请求/my,而是决定


:?

只需颠倒条件即可:

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

这是一个从旧客户网站上获得的功能,可以根据您的目的进行调整:

#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]

将购物车替换为my Problem

尝试以下方法,应该对您有用:

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]

啊,当然。问题在于,您的重写规则集在初始重定向后转换为
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

你好,谢谢你的快速回复。我试过了,但我不确定应该把它放在我现有规则的流程中,有什么指针吗?如果我将其置于现有的“强制HTTPS”规则之上,它会阻止“强制HTTPS”工作,也会阻止重写index.php的最终规则执行。:@内森·皮特曼:不,因为他们有不同的匹配条件,所以他们不应该互动。好吧,我试了你的建议(见我原来帖子的更新),但这不起作用。我可以看到它“应该”,但我认为可能是规则顺序错误,或者某些规则导致重写在其他条件和规则可以评估之前终止…:/@Nathan Pitman:或者你没有提到你在
httpd.conf
(或类似文件)中定义规则,每个人都假设它在
.htaccess
,在这种情况下,你需要确保所有的测试模式都匹配
^/
,例如
^/my
(我可能是错的,这只是一个想法)Hi Tim,是的,这是在中定义的。谢谢,节省了我很多时间!FWIW,在Wordpress中,基本Wordpress htaccess IfModule已经从URL中删除index.php。