使用正则表达式的 htaccess 重写条件

发布于 2025-01-19 05:35:21 字数 446 浏览 0 评论 0原文

当涉及到正则是菜鸟。我正在尝试完成的是:

https://www.example.com/shop/product-floation-floating-front-rotor-kit/

应该重定向到

https://www。 example.com/shop/product-matching-front-rotor/

product 应该是产品的名称,我必须为多种产品执行此操作。

编辑:这是我到目前为止所拥有的,我什至接近了吗?

RewriteEngine On

RewriteRule ^/shop/([a-z]+)-floating-front-rotor-kit/ ^/shop/$1-matching-front-rotor/

I'm a noob when it comes to regex. What I'm trying to accomplish is:

https://www.example.com/shop/product-floating-front-rotor-kit/

should redirect to

https://www.example.com/shop/product-matching-front-rotor/

product should be the name of the product, I have to do this for multiple products.

Edit: This is what I have so far, am I even close?

RewriteEngine On

RewriteRule ^/shop/([a-z]+)-floating-front-rotor-kit/ ^/shop/$1-matching-front-rotor/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

梦巷 2025-01-26 05:35:21
 重写 ^/shop/([az]+) - 浮动 - 前旋转 -  kit/ ^/shop/$ 1搭配 -  front-front-rotor/
 

这是接近的,除了

  • 中的...

    。不是从斜线开始。

  • 替换字符串具有错误的^前缀。这应该是一个“普通”字符串,而不是正则弦。

  • [az]不匹配连字符/破折号,您可以在产品名称中出现。

  • 您尚未在重写 stater将成功并丢弃。 (这是意图吗?)


  • 这是内部的“重写”,而不是所述的“重定向”。您需要包括r标志。 (由于目标URL需要进一步重写,因此内部重写不太可能在这里工作。)

尝试以下内容。这应该在.htaccess文件的顶部,rewriteEngine指令之后。

RewriteRule ^shop/([a-z-]+)-floating-front-rotor-kit/$ /shop/$1-matching-front-rotor/ [R=302,L]

这是302(临时)重定向。

RewriteRule ^/shop/([a-z]+)-floating-front-rotor-kit/ ^/shop/$1-matching-front-rotor/

This is close, except that...

  • In .htaccess the URL-path matched by the RewriteRule pattern (first argument) does not start with a slash.

  • The substitution string has an erroneous ^ prefix. This should be an "ordinary" string, not a regex.

  • [a-z] does not match hyphens/dashes, which you state could occur in a product name.

  • You have not included an end-of-string anchor ($) on the end of the RewriteRule pattern, so any trailing string will be successful and discarded. (Is that the intention?)

  • This is an internal "rewrite", not a "redirect" as stated. You need to include the R flag. (An internal rewrite is unlikely to work here, since the target URL requires further rewriting.)

Try the following instead. This should go at the top of the .htaccess file, immediately after the RewriteEngine directive.

RewriteRule ^shop/([a-z-]+)-floating-front-rotor-kit/$ /shop/$1-matching-front-rotor/ [R=302,L]

This is a 302 (temporary) redirect.

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