regex - 从 http 重定向到 https 时 mod_rewrite 中的无限重定向
问题描述
.htaccess 文件如下所示,可以很好地将流量从 website.com/page 或 website.com/page/ 重定向到 index.php?site=page,从 website.com/page/subpage/ 重定向到 index.php ?site=page&sub=子页面。
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteRule ^([^/]+)$ index.php?site=$1 [QSA]
RewriteRule ^([^/]+)/$ index.php?site=$1 [QSA]
RewriteRule ^([^/]+)/([^/]+)/$ index.php?site=$1&sub=$2 [QSA]
尝试将流量从 http:// 重定向到 https:// 时出现问题
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R,L]
我收到一条消息,指出网站上的重定向太多,无论 url 是什么。你知道出了什么问题吗?
解决方案
你的规则应该是这样的:
RewriteEngine On
RewriteCond %{REQUEST_SCHEME} =http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,NE,L]
RewriteRule ^([^/.]+)/?$ index.php?site=$1 [QSA,L]
RewriteRule ^([^/.]+)/([^/.]+)/?$ index.php?site=$1&sub=$2 [QSA,L]
确保在新浏览器中进行测试以避免旧浏览器缓存。
推荐阅读
- vue.js - VueJS: vs or related for grouping elements for conditional rendering
In Vue.js, if you want to conditionally render multiple elements via a
v-if/v-else-if
directive you can wrap them inside<template
- javascript - Promise 在 Node 中没有解决无限 while 循环?
- angularjs - 调试时 AngularJS 1.5 文件上传在 IE/Chrome 中工作
- kotlin - 将不同的模型合并到密封类中
- kubernetes - 如何使用 AKS 和 AAD 集成指定回复 URL?
- c++ - 在 C++ 中设置/暂停 std::chrono 时钟的时钟时间
- ember.js - Ember-Drag-Sort:自定义 HTML 元素
- java - 如何让 JavaFx Combobox 响应用户输入?
- javascript - 可以使用 jQuery 在 2 个 FontAwesome 5 图标(常规和实心)之间切换
- java - 警告:没有处理器声明任何这些注释:javax.annotation.Generated