使用 Apache 将除单个 URL 之外的所有流量重定向到 https

使用 Apache 将除单个 URL 之外的所有流量重定向到 https

我希望所有到非 https example.com 和 www.example.com 的流量都重定向到 https www.example.com,但某些路径除外,即http://www.example.com/import , /import/XXX, /import/YYY 应该保持 http。

目前,我正在使用以下内容将所有非 https 流量重定向到 https。

<VirtualHost *:80>
        ServerName www.example.com

        Redirect permanent / https://www.example.com/
</VirtualHost>

我想,当我尝试重写并最终陷入循环时,大多数问题都来自于此。

更新

我已启用 mod_rewrite 日志记录并得到结果:

... (2) init rewrite engine with requested uri /import/batch_name
... (3) applying pattern '^(.*)$' to uri '/import/batch_name'
... (1) pass through /import/batch_name
... (3) [perdir /var/www/] add path info postfix: /var/www/import -> /var/www/import/batch_name
... (3) [perdir /var/www/] strip per-dir prefix: /var/www/import/batch_name -> import/batch_name
... (3) [perdir /var/www/] applying pattern '^' to uri 'import/batch_name'
... (3) [perdir /var/www/] add path info postfix: /var/www/import -> /var/www/import/batch_name
... (3) [perdir /var/www/] strip per-dir prefix: /var/www/import/batch_name -> import/batch_name
... (3) [perdir /var/www/] applying pattern '^' to uri 'import/batch_name'
... (3) [perdir /var/www/] add path info postfix: /var/www/import -> /var/www/import/batch_name
... (3) [perdir /var/www/] strip per-dir prefix: /var/www/import/batch_name -> import/batch_name
... (3) [perdir /var/www/] applying pattern '^' to uri 'import/batch_name'
... (3) [perdir /var/www/] add path info postfix: /var/www/import -> /var/www/import/batch_name
... (3) [perdir /var/www/] strip per-dir prefix: /var/www/import/batch_name -> import/batch_name
... (3) [perdir /var/www/] applying pattern '(^|/)\.' to uri 'import/batch_name'
... (3) [perdir /var/www/] add path info postfix: /var/www/import -> /var/www/import/batch_name
... (3) [perdir /var/www/] strip per-dir prefix: /var/www/import/batch_name -> import/batch_name
... (3) [perdir /var/www/] applying pattern '^' to uri 'import/batch_name'
... (2) [perdir /var/www/] rewrite 'import/batch_name' -> 'index.php'
... (3) [perdir /var/www/] add per-dir prefix: index.php -> /var/www/index.php
... (2) [perdir /var/www/] strip document_root prefix: /var/www/index.php -> /index.php
... (1) [perdir /var/www/] internal redirect with /index.php [INTERNAL REDIRECT]
... (2) init rewrite engine with requested uri /index.php
... (3) applying pattern '^(.*)$' to uri '/index.php'
... (2) rewrite '/index.php' -> 'https://www.example.com/index.php'
... (2) explicitly forcing redirect with https://www.example.com/index.php
... (1) escaping https://www.example.com/index.php for redirect
... (1) redirect to https://www.example.com/index.php [REDIRECT/301]

答案1

您可以尝试如下的 Apache 重写规则。

RewriteEngine On
RewriteCond %{REQUEST_URI}  !(import\/batch_name1|import\/batch_name2|import\/batch_name3) [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,QSA]

相关内容