htaccess 重定向到 HTTPS,除了几个 url [英] htaccess redirect to HTTPS except a few urls
问题描述
我是 htaccess 重定向的新手,但想做一些特别的事情 - 我不知道推荐的方式是什么,也不知道这是否仍然可能.
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 文件中有这个:
I have this in my .htaccess file:
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 版本 - 这很好而且很有必要.但现在有一些例外.
Now every URL is redirected to the HTTPS version - this is fine and necessery. But now there are a few exceptions.
例如,这些网址必须是 HTTP 而不是 HTTPS:
For example these urls HAS to be HTTP instead of HTTPS:
http://www.mywebsite.com/another/url/which/has/to/be/http
http://www.mywebsite.com/and_again?var=a
是否可以使用 htaccess 解决这个问题,如果可能,您可以向我发送参考链接或描述如何执行此操作.
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.
编辑
我现在有这个代码:
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_paypal 开头的 url 除外.
The goal is that every (!) url gets redirected to HTTPS except ANY url which has commerce_paypal at the beginning.
例如:
mydomain.com/commerce_paypal <- http
mydomain.com/commerce_paypal/smth/else <- http
mydomain.com/what/ever <- https
推荐答案
您可以使用 RewriteCond
在 http->http
规则中添加异常:>
You can have a RewriteCond
to add exceptions in the http->http
rule:
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]
这篇关于htaccess 重定向到 HTTPS,除了几个 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!