Regex htaccess重定向到HTTPS,除了少数URL

Regex htaccess重定向到HTTPS,除了少数URL,regex,apache,.htaccess,mod-rewrite,redirect,Regex,Apache,.htaccess,Mod Rewrite,Redirect,我是新的htaccess重定向的东西,但想做smth特别-我不知道什么是推荐的方式,也不知道这是否仍然是可能的 我的.htaccess文件中有以下内容: RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUES

我是新的htaccess重定向的东西,但想做smth特别-我不知道什么是推荐的方式,也不知道这是否仍然是可能的

我的.htaccess文件中有以下内容:

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
现在,每个URL都被重定向到HTTPS版本-这很好,也很必要。但现在也有一些例外

例如,这些URL必须是HTTP而不是HTTPS:

http://www.mywebsite.com/another/url/which/has/to/be/http
http://www.mywebsite.com/and_again?var=a
有没有可能通过htaccess解决这个问题,如果可能的话,您可以给我发一个参考链接或描述如何做到这一点

编辑

我现在有了这个代码:

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTPS} off 
RewriteCond %{THE_REQUEST} !\s/+(/commerce_paypal/*)\s [NC] 
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
我们的目标是每个(!)url都被重定向到HTTPS,除了任何一个开头有commerce\u paypal的url

例如:

mydomain.com/commerce_paypal  <- http
mydomain.com/commerce_paypal/smth/else <- http
mydomain.com/what/ever <- https

mydomain.com/commerce\u paypal您可以使用
RewriteCond
http->http
规则中添加异常:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# force https:// for all except some selected URLs    
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !/commerce_paypal/ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# force http:// for selected URLs
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} /commerce_paypal/ [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
参考:
为我工作。我尝试了许多类似的条件重写,但运气不佳。

谢谢你的回答。我现在有了这个片段来强制执行异常:RewriteCond%{HTTPS}关闭RewriteCond%{the_REQUEST}\s/+(/commerce\u paypal/*)\s[NC]重写规则^https://%{HTTP\u HOST}%{REQUEST\u URI}[L,R=301]正如您所看到的,计划是强制commerce\u paypal不再是https。有一些commerce\u paypal url,所以我输入了“commerce\u paypal/*”,这样每个url都可以在/之后,但必须是HTTP。但这不起作用。。。有什么想法吗?你可以在问题中发布代码,而不是评论,以使其可读。嗯,你的正则表达式不正确。请参阅我的更新答案和更正版本。非常感谢!这帮了我:)太好了@anubhava,您的解决方案在本地为我工作,但我的登台环境在负载平衡器后面,并且陷入无限重定向循环中。我添加了
RewriteCond%{HTTP:X-Forwarded-Proto}!https
捕获除我的一个排除的URL之外的所有URL并将其发送到https。然后在将我的一个URL重定向到HTTP的第二个块中,我根据此处的建议添加了
RewriteCond%{HTTP:X-Forwarded-Proto}=https
RewriteCond %{THE_REQUEST} !/commerce_paypal/ [NC]