htaccess重定向到HTTPS,除了几个网址

TJR*_*TJR 11 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}%{REQUEST_URI} [L,R=301]
Run Code Online (Sandbox Code Playgroud)

现在每个URL都被重定向到HTTPS版本 - 这很好并且必要.但现在有一些例外.

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

http://www.mywebsite.com/another/url/which/has/to/be/http
http://www.mywebsite.com/and_again?var=a
Run Code Online (Sandbox Code Playgroud)

是否有可能使用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]
Run Code Online (Sandbox Code Playgroud)

目标是每个(!)url都被重定向到HTTPS,除了任何在开头都有commerce_paypal的url.

例如:

mydomain.com/commerce_paypal  <- http
mydomain.com/commerce_paypal/smth/else <- http
mydomain.com/what/ever <- https
Run Code Online (Sandbox Code Playgroud)

anu*_*ava 23

您可以RewriteCondhttp->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]
Run Code Online (Sandbox Code Playgroud)

参考:Apache mod_rewrite简介

Apache mod_rewrite技术细节

  • 非常感谢你!这帮助了我:)太棒了! (3认同)