.htaccess 在某些URL上添加SSL(可选)

.htaccess 在某些URL上添加SSL(可选),.htaccess,ssl,apache2,vhosts,apache2.4,.htaccess,Ssl,Apache2,Vhosts,Apache2.4,我正在使用Apache2作为web应用程序 我想强制大多数URL使用SSL。e、 g.如果我转到http://example.com/foo我将被重定向到https://example.com/foo 通过使用以下工具,这非常简单: <VirtualHost *:80> ServerName example.com ServerAlias *.example.com RewriteEngine on RedirectMatch ^(/.*)$ http

我正在使用Apache2作为web应用程序

我想强制大多数URL使用SSL。e、 g.如果我转到
http://example.com/foo
我将被重定向到
https://example.com/foo

通过使用以下工具,这非常简单:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias *.example.com
    RewriteEngine on
    RedirectMatch ^(/.*)$ https://example.com$1
</VirtualHost>
原因是我的手机应用已经上线,并且使用了网站的http版本,我不想破坏它


如何执行此操作?

在重写规则之前加上

RewriteCond %{REQUEST_URI} !^/api/

我有以下代码:
RewriteCond%{REQUEST\u URI}^/api/RewriteRule^(/.*)$https://%{SERVER_NAME}$1[R,L]
,但当我转到
http://example.com/api/offer/feed
,它将重定向到
http://example.com/index.php
。它为什么要这样做?不相关的规则?只有其他规则可以在中找到。我真的很困惑如何解决这个问题,因为我迫切需要解决它!当我删除htaccess文件时,它工作正常。但我不明白为什么会发生这种行为。这是Apache的错误吗?我接受了正确的答案。我对htaccess文件的问题将被放在另一个问题中
RewriteCond %{REQUEST_URI} !^/api/