You need to enable JavaScript to run this app. CentOS下多条件同路径htaccess重定向规则配置问题求助
优惠活动
大模型
产品
解决方案
定价
更多

CentOS下多条件同路径htaccess重定向规则配置问题求助

配置问题排查
  • 语法错误:开头多余</IfModule>闭合标签,会导致规则加载失败
  • 路径匹配逻辑错误:使用THE_REQUEST匹配路径的方式不符合变量特性,且^/view/的匹配规则无法命中/admin/xxxx/view/格式的路径
  • 规则覆盖不全:完全没有处理/display相关路径的跳转逻辑,也没有覆盖根路径直接访问的跳转需求
  • 规则顺序错误:需要跳转到HTTP的规则放在了HTTPS跳转规则之后,会被优先级更高的前置HTTPS跳转规则截断,无法生效
修正后的完整.htaccess配置
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On

# 规则1:https访问example.org的/display路径,跳转到http example.intra对应路径
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^example\.org$ [NC]
RewriteRule ^display(/.*)?$ http://example.intra%{REQUEST_URI} [R=301,L,NC]

# 规则2:https访问example.org的/admin下包含view的路径,跳转到http example.intra对应路径
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^example\.org$ [NC]
RewriteRule ^admin/.*?/view(/.*)?$ http://example.intra%{REQUEST_URI} [R=301,L,NC]

# 规则3:http访问example.intra的根路径或/admin路径,跳转到https example.org/admin
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^example\.intra$ [NC]
RewriteRule ^(admin)?$ https://example.org/admin [R=301,L,NC]

# 规则4:http访问example.intra的admin路径(不含view的),跳转到https example.org对应路径
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^example\.intra$ [NC]
RewriteCond %{REQUEST_URI} !/view/ [NC]
RewriteRule ^admin(/.*)?$ https://example.org%{REQUEST_URI} [R=301,L,NC]

# 规则5:http访问example.org的任意路径,强制跳https
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^example\.org$ [NC]
RewriteRule ^(.*)$ https://example.org%{REQUEST_URI} [R=301,L,NC]

# 原有业务规则
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
RewriteRule !^(ui/|index.php|favicon.ico) - [F,L,NC]
</IfModule>
规则生效验证说明
  • 先清空浏览器缓存,301跳转是永久跳转,旧缓存会影响测试结果
  • 按需求场景逐一测试:
    1. 访问http://example.intra/display/xxx 不会跳转,保持HTTP
    2. 访问http://example.intra/ 或 http://example.intra/admin 都会跳转到https://example.org/admin
    3. 访问https://example.org/display/xxx 会跳转到http://example.intra/display/xxx
    4. 访问https://example.org/admin/xxx/view/xxx 会跳转到http://example.intra/admin/xxx/view/xxx
    5. 其他不含view的admin路径访问example.intra的HTTP站点会跳转到example.org的HTTPS站点

内容的提问来源于stack exchange,提问作者Amin Afip

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.13 19:27:03