首页 > 解决方案 > 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]

我会很感激你的帮助。

标签: apache.htaccesshttphttps

解决方案


自己找到了答案

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

推荐阅读