.htaccess 头疼:将根文件夹重定向到 A,将这些文件夹重定向到 B,忽略其余部分

.htaccess headache: redirect root folder to A, these folders to B, ignore the rest

该网站的结构是在根文件夹中包含主要语言 (fi),在子文件夹中包含其他语言版本。该站点由真实文件夹中的静态 HTML 文件组成,而非虚拟文件夹。在这里,我在第一行有一个有效的规则,并重定向以这三个文件夹为目标的所有流量。然而,第二行没有做我想要的。

RedirectMatch permanent "(se|pl|en)\/(.+)?" https://www.example.com/example
RedirectMatch permanent "(!lv|!ee)\/?(.+)?" https://www.example.com/another-example

如果我理解正确,您可以将这些规则与正则表达式锚点一起使用:

RewriteEngine On

# se|pl|en redirection
RewriteRule ^(se|pl|en) https://www.example.com/example/ [L,R=302,NC]

# anything other than /ee and /lv
RewriteRule !^(ee|lv)/ https://www.example.com/another-example/ [L,R=302,NC]

确保在测试这些更改之前清除浏览器缓存。

我开始工作时忘记了 RedirectMatch,而是使用 RewriteCond + RewriteRule

RewriteEngine On

RewriteCond %{REQUEST_URI} ^/(se|pl|en)/
RewriteRule ^(.*)$ https://www.example.com/example/ [L,R=302]

RewriteCond %{REQUEST_URI} !/(ee|lv)/ [NC]
RewriteRule ^(.*)$ https://www.example.com/another-example/ [L,R=302]

应用自 anubhava 对这个问题的回答: