问题描述
我有一个客户端项目,我需要为某个文件夹强制使用 HTTPS,并为所有其他文件夹强制使用 HTTP.我可以成功地为我想要的文件夹强制执行 HTTPS,但是所有返回站点其余部分的链接最终都通过 HTTPS.我想有一个规则,强制对安全文件夹中的任何非"请求强制返回 HTTP.这是我到目前为止所拥有的:
I have a client project where I need to force HTTPS for a certain folder and force HTTP for all others. I can sucessfully enforce HTTPS for the folder I desire but then all links back to the rest of the site end up being through HTTPS. I'd like to have a rule which forces requests for anything 'not' in the secure folder to be forced back to HTTP. Here's what I have so far:
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 的文件夹的名称.
'my' is the name of the folder that I need to force HTTPS for.
有什么想法吗?
更新:我也试过:
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,它们现在只是解析为 http://www.example.com/index.php/my
But instead of requests for /my being forced through HTTPS they now just resolve to http://www.example.com/index.php/my
:?
推荐答案
啊,当然.问题在于您的重写规则集将在初始重定向后转换为 index.php
后被重新处理.使用您当前拥有的内容,您需要额外调整重定向以确保在重写到 /index.php/my
后不会应用它们.
Ah, of course. The problem lies in the fact that your rewrite ruleset will be reprocessed after it is transformed to index.php
following the initial redirect. Using what you currently have, you need to additionally condition the redirections to make sure they don't get applied after the rewrite to /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
这篇关于对某些 URL 强制使用 HTTPS,对所有其他 URL 强制使用 HTTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!