使用正则表达式的 htaccess 重写条件
当涉及到正则是菜鸟。我正在尝试完成的是:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是接近的,除了
中的...
。不是从斜线开始。
替换字符串具有错误的
^
前缀。这应该是一个“普通”字符串,而不是正则弦。[az]
不匹配连字符/破折号,您可以在产品名称中出现。您尚未在
重写
stater将成功并丢弃。 (这是意图吗?)这是内部的“重写”,而不是所述的“重定向”。您需要包括
r
标志。 (由于目标URL需要进一步重写,因此内部重写不太可能在这里工作。)尝试以下内容。这应该在
.htaccess
文件的顶部,rewriteEngine
指令之后。这是302(临时)重定向。
This is close, except that...
In
.htaccess
the URL-path matched by theRewriteRule
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 theRewriteRule
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 theRewriteEngine
directive.This is a 302 (temporary) redirect.