没有 URL 最后一部分的 RedirectMatch

RedirectMatch without last part of URL

我有这个RedirecMatch

RedirectMatch 301 ^/en/products/(.*)/(.*)/(.*)$ https://www.example.com/en/collections//

如果我访问

https://www.example.com/en/products/sofas/greyson/greyson-sofa

我被重定向到

https://www.example.com/en/collections/greyson/greyson-sofa

我要的是

https://www.example.com/en/collections/greyson/

我该如何完成?

您发布的内容中没有明显的内容会产生您所看到的特定输出,但是,指令中还有其他错误,您可能会看到缓存的响应。 301 由浏览器持久缓存,因此任何错误也被缓存。

Redirect 指令是 prefix-matching,匹配后的所有内容都被复制到目标 URL 的末尾。因此,您看到的重定向将由如下指令生成:

Redirect 301 /en/products/sofas/greyson https://www.example.com/en/collections/sofas/greyson

当你请求/en/products/sofas/greyson/greyson-sofa时,匹配后的部分,即。 /greyson-sofa,被复制到目标 URL 的末尾以产生 /en/collections/sofas/greyson/greyson-sofa

您可以通过重新排序规则来解决大部分问题(但也要注意结尾的斜线)。您首先需要最具体的重定向。 RedirectMatch之前Redirect。例如,采用以下两个重定向:

Redirect 301 /en/products/accessories https://www.example.com/en/products/complements/
Redirect 301 /en/products/accessories/bush/ https://www.example.com/en/collections/bush-on/

由于 Redirect 指令是 prefix-matching,对 /en/products/accessories/bush/ 的请求实际上会被第一条规则捕获,而不是第二条规则,并最终重定向到 /en/products/complements//bush-on/ - 注意错误的 double-slash(因为源和目标 URL 上的尾部斜杠不匹配。)

你需要颠倒这两条规则。 (但也要注意结尾的斜杠。)

这同样适用于后面的 Redirect 指令。你也有一些重复,即。 /en/products/chairs-and-bar-stools/piper/?

有两个规则