retirectMatch没有URL的最后一部分
我有这个 RedirecMatch
RedirectMatch 301 ^/en/products/(.*)/(.*)/(.*)$ https://www.example.com/en/collections/$2/
如果我访问,
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/
如何完成此操作?
I have this RedirecMatch
RedirectMatch 301 ^/en/products/(.*)/(.*)/(.*)$ https://www.example.com/en/collections/$2/
If I visit
https://www.example.com/en/products/sofas/greyson/greyson-sofa
I'm redirected to
https://www.example.com/en/collections/greyson/greyson-sofa
What I want is
https://www.example.com/en/collections/greyson/
How do I accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您发布的内容中没有任何明显的内容可以产生您所看到的特定输出,但是,指令中存在其他错误,并且您可能会看到缓存的响应。 301 由浏览器永久缓存,因此任何错误也会被缓存。
Redirect
指令是前缀匹配的,匹配后的所有内容都会复制到目标 URL 的末尾。因此,您看到的重定向将由如下指令生成:当您请求
/en/products/sofas/greyson/greyson-sofa
时,匹配后的部分,即。/greyson-sofa
被复制到目标 URL 的末尾以生成/en/collections/sofas/greyson/greyson-sofa
您可以通过以下方式解决大多数此类问题重新排序你的规则(但也要注意结尾的斜杠)。您首先需要有最具体的重定向。
RedirectMatch
之前重定向
。例如,采用以下两个重定向:由于
Redirect
指令是前缀匹配的,因此对/en/products/accessories/bush/
的请求实际上会被第一个规则捕获,而不是第二个规则,并最终重定向到/en/products/complements//bush-on/
- 请注意错误的双斜杠(因为源 URL 和目标 URL 上的尾部斜杠不匹配。)您需要反转这些二规则。 (但还要注意结尾的斜杠。)
这同样适用于后面的
Redirect
指令。你也有一些重复,即。您对/en/products/chairs-and-bar-stools/piper/
有两条规则?There's nothing obvious in what you have posted that would produce the specific output you are seeing, however, there are other errors in the directives and you may be seeing a cached response. 301s are cached persistently by the browser, so any errors are also cached.
The
Redirect
directive is prefix-matching and everything after the match is copied onto the end of the target URL. So, the redirect you are seeing would be produced by a directive something like this:When you request
/en/products/sofas/greyson/greyson-sofa
, the part after the match, ie./greyson-sofa
, is copied onto the end of the target URL to produce/en/collections/sofas/greyson/greyson-sofa
You can resolve most of these issues by reordering your rules (but also watch the trailing slashes). You need to have the most specific redirects first.
RedirectMatch
beforeRedirect
. For example, take the following two redirects:Since the
Redirect
directive is prefix-matching, a request for/en/products/accessories/bush/
will actually be caught by the first rule, not the second and end up redirecting to/en/products/complements//bush-on/
- note the erroneous double-slash (since you have a mismatch of trailing slashes on the source and target URLs.)You need to reverse these two rules. (But also watch the trailing slash.)
The same applies to the
Redirect
directives that follow. You also have some duplication, ie. You have two rules for/en/products/chairs-and-bar-stools/piper/
?