apache - htaccess 重定向到 https 排除一个 url
问题描述
我知道它的重复,谷歌有很多答案,但对我来说没有任何效果。
我对 apache 不强,甚至不平均,但我想排除一个/test/
从 HTTPS 重定向开始的网址。
//redirect not working
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^\/(/test/)
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
RewriteCond %{HTTP:X-Forwarded-Proto} https
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^\/(/test/)
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
//infinity loop
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{REQUEST_URI} ^\/(test)
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTP:X-Forwarded-SSL} =on
RewriteCond %{REQUEST_URI} !^\/test)
RewriteRule (.*) http://%{HTTP_HOST}/$1 [L,R=301]
//thats my first HTTPS redirect, tried to add RewriteCond to exclude test, but nothing works
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/test/ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
我会很感激你的帮助。
解决方案
自己找到了答案
# force https:// for all except some selected URLs
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !/test/ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# force http:// for selected URLs
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} /test/ [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
推荐阅读
- java - 将 JAX-RS Web 服务与 Google App Engine 一起使用时,FilteringJacksonJaxbJsonProvider 出现 NullPointerException
- html - 使用 href="#" 使页面滚动到顶部
- python - 找到字典中的元素(包含在列表中)后如何停止列表中的 for 循环
- node.js - 使用 Node.js 实现推送通知
- kalman-filter - 验证卡尔曼滤波器的答案
- python - 如何在 wx.dataview.TreeListCtrl 中骑空方块
- javascript - 覆盖react datepicker popper className的样式
- c# - RadGrid 中的超链接 onclick
- java - 将窗口最小化到 JavaFX 应用程序时图像消失
- angular - 如何在 ng2-table 组件中使用 i18n?