简体   繁体   English

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

[英]htaccess redirect to HTTPS except a few urls

I am new to the htaccess redirect stuff but want to do smth special - and I dont know whats the recommend way and dont know if this is still possible or not. 我是htaccess重定向的新手,但是想要做特别的smth - 我不知道什么是推荐的方式,不知道这是否仍然可行。

I have this in my .htaccess file: 我在.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]

Now every URL is redirected to the HTTPS version - this is fine and necessery. 现在每个URL都被重定向到HTTPS版本 - 这很好并且必要。 But now there are a few exceptions. 但现在有一些例外。

For example these urls HAS to be HTTP instead of HTTPS: 例如,这些URL必须是HTTP而不是HTTPS:

http://www.mywebsite.com/another/url/which/has/to/be/http
http://www.mywebsite.com/and_again?var=a

Is it possible to solve this with the htaccess and when its possible maybe you can send me a reference link or describe how to do this. 是否有可能使用htaccess来解决这个问题,当它可能时,您可以发送参考链接或描述如何执行此操作。

Edit 编辑

I now have this code: 我现在有这个代码:

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]

The goal is that every (!) url gets redirected to HTTPS except ANY url which has commerce_paypal at the beginning. 目标是每个 (!)url都被重定向到HTTPS,除了任何在开头都有commerce_paypal的url。

For example: 例如:

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

You can have a RewriteCond to add exceptions in the http->http rule: 您可以使用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]

Reference: Apache mod_rewrite Introduction 参考: Apache mod_rewrite简介

Apache mod_rewrite Technical Details Apache mod_rewrite技术细节

RewriteCond %{THE_REQUEST} !/commerce_paypal/ [NC]

worked for me. 为我工作。 I tried many similar condition rewrites without luck. 没有运气,我尝试了很多类似的条件重写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM