Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.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
htaccess:在所有页面上强制使用http,在选定目录上强制使用https_Http_.htaccess_Mod Rewrite_Https - Fatal编程技术网

htaccess:在所有页面上强制使用http,在选定目录上强制使用https

htaccess:在所有页面上强制使用http,在选定目录上强制使用https,http,.htaccess,mod-rewrite,https,Http,.htaccess,Mod Rewrite,Https,我有以下资料: RewriteCond %{HTTPS} off RewriteCond %{REQUEST_URI} protected [NC] RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301] RewriteCond %{HTTPS} on RewriteCond %{REQUEST_URI} !protected [NC] RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [L,R=301] 如

我有以下资料:

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

RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !protected [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [L,R=301]
如果目录被称为“受保护”,请确保用户正在使用https。如果目录不是“受保护的”,请确保用户正在使用http

这很好,但是如何指定其他目录

另外,有没有一种方法可以在不必指定两次目录的情况下实现这一点?一次包含它,一次排除它

谢谢

更新

尽管由于我的规则,我的“受保护”文件夹被迫使用https,但对不在“受保护”文件夹中的图像、样式表和Java脚本的任何引用仍被重定向到http。这将导致“受保护”页面仅部分安全。在重定向代码之前添加以下内容可以解决此问题:

RewriteRule \.(css|gif|jpe?g|js|png|swf)$ - [L]

我在vhost配置中执行此操作(
LocationMatch
在htaccess中不可用,但这样您可以确保不会意外删除它):
(注意:用您的服务器替换
\uuuuuuuuuuuu服务器
,它不是自动的。)


...
重定向永久/https://__SERVER__/
...
重定向永久/http://__SERVER__/
我从未测试过第二个场景(重定向到非安全),但它应该可以工作(不确定
位置)。
我还没有找到一种不两次指定它们的好方法,但是为
LocationMatch

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} protected [NC,OR]
RewriteCond %{REQUEST_URI} protected2 [NC,OR]
RewriteCond %{REQUEST_URI} protected3 [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]

RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !protected [NC]
RewriteCond %{REQUEST_URI} !protected2 [NC]
RewriteCond %{REQUEST_URI} !protected3 [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [L,R=301]
您可以使用
添加更多选项



以下是有关mod_重写条件的更多详细信息:

如果不限于.htaccess,什么是更好的解决方案?如果您使用WordPress,则需要在默认WordPress重写之前放置这些选项,否则它们可能无法按预期工作!
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} protected [NC,OR]
RewriteCond %{REQUEST_URI} protected2 [NC,OR]
RewriteCond %{REQUEST_URI} protected3 [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]

RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !protected [NC]
RewriteCond %{REQUEST_URI} !protected2 [NC]
RewriteCond %{REQUEST_URI} !protected3 [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [L,R=301]