apache - 如何在 htaccess 中将除一个页面之外的所有页面定向到 HTTPS?

标签 apache .htaccess redirect mod-rewrite laravel-4

目前,我使用以下 htaccess 指令将所有页面重定向到 https:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

但是我有一个页面,http://foo.bar/baz,需要仅通过 http 访问。使用 htaccess 指令,如何在除此页面之外的所有页面上重定向到 https,用户被迫使用 http?

编辑:我应该提到这是一个 Laravel 4 应用程序,我目前也有以下规则来处理前端 Controller

# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

最佳答案

您可以在RewriteRule中使用负正则表达式模式:

RewriteEngine On

RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !/baz[/?\s] [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,NE,L]

RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} /baz[/?\s] [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,NE,L]

RewriteRule ^index\.php - [L,NC]

# Redirect Trailing Slashes...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301,NE]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

关于apache - 如何在 htaccess 中将除一个页面之外的所有页面定向到 HTTPS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39148197/

相关文章:

java - 在 MacOS 上设置 JAVA_HOME 和 PATH

html - 在您的根目录中有一个索引页面是否至关重要,例如 index.html、index.php

php - 如何密码保护 index.php 和/,但允许访问所有其他文件和子目录。

ajax - 为什么FullAjaxExceptionHandler不能简单地执行ExternalContext#redirect()?

apache - 让 .htaccess RewriteRule 重定向到脚本 "in current dir"(而不是显式路径)

apache - 配置域范围的 WebLogic 错误页面

java - 似乎无法让从我的 Android 应用程序发送的 http 获取参数显示在我的 PHP 页面上

apache - 在 MAMP 中的文件夹上将 http 转发到 https

regex - 301 重定向仅适用于 .asp,不适用于 .aspx

javascript - Chrome 扩展程序会重定向每一帧