retirectMatch没有URL的最后一部分

发布于 2025-01-17 16:21:42 字数 457 浏览 4 评论 0原文

我有这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷迭香的记忆 2025-01-24 16:21:42

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

Redirect 指令是前缀匹配的,匹配后的所有内容都会复制到目标 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 之前 重定向。例如,采用以下两个重定向:

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

由于 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:

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

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 before Redirect. For example, take the following two redirects:

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/

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/?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文