htaccess重定向到https排除一个URL

时间:2018-10-08 13:54:58

标签: apache .htaccess http https

我知道它的重复项,并且在google中有很多答案,但对我来说仍然无效。
我对Apache的了解不强,甚至都不是平均水平,但是我想从HTTPS重定向中排除一个以/test/开头的网址。

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

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

自己找到答案

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