You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用.htaccess重定向MODX多语言站点子文件夹根目录而非其子项

解决MODX多语言站点/fi/根目录重定向循环问题

问题原因

你添加的RedirectMatch 301 /fi /fi/something会匹配所有以/fi开头的路径(比如/fi/something、/fi/products),导致重定向到/fi/something后,再次触发该规则,形成无限循环。

正确解决方案

使用精确匹配的RewriteRule规则,只针对/fi或/fi/执行重定向,不会影响子路径。将以下规则添加到.htaccess的RewriteBase /之后、语言检测规则之前:

# 仅重定向/fi或/fi/到/fi/something
RewriteRule ^fi/?$ /fi/something [R=301,L]

修改后的完整.htaccess代码

RewriteEngine On
Options +FollowSymLinks
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
RewriteBase /

# 仅重定向/fi或/fi/到/fi/something
RewriteRule ^fi/?$ /fi/something [R=301,L]

#Check for Fi
RewriteCond %{HTTP_HOST} .*example.com [NC]
RewriteCond %{HTTP:Accept-Language} ^fi [NC]
RewriteRule ^$ https://%{HTTP_HOST}/fi/ [L,R=301]

# Default to En
RewriteCond %{HTTP_HOST} .*example.com [NC]
RewriteRule ^$ https://%{HTTP_HOST}/en/ [L,R=301]

# Fix current links
RewriteCond %{HTTP_HOST} example.com [NC]
RewriteCond %{REQUEST_URI} !/en
RewriteCond %{REQUEST_URI} !/fi
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /en/$1 [L]

# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

规则说明

  • ^fi/?$:^匹配路径开头,fi是目标前缀,/?匹配可选的末尾斜杠,$匹配路径结尾,确保只匹配/fi或/fi/。
  • [R=301,L]:R=301表示永久重定向,L表示这是最后一条规则,避免后续规则干扰。

内容的提问来源于stack exchange,提问作者Ville Teronen

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 01:10:54