替换 URL 中任意位置的关键字并 301 重定向到新位置

Replace keyword in anywhere in URL and 301 Redirect to new location

我们有很多 url 的结构类似于(我知道这很糟糕)

.com/sale/product-x
.com/product-category/product-x
.com/product-x
.com/product-category/sub-category/product-x

实际上有几十万个...

我们希望使用 mod_rewrite 能够将 "product-x" 替换为 "productx"

如您所见,"product-x" 可以在 url 中的任何位置 - 我们基本上想要的是仅针对关键字 "product-x"[= 的 "find-replace" 类函数12=]

--

我们如何才能使用 .htaccess 文件来创建所需的效果?

谢谢

您可以将此规则用作站点根目录 .htaccess 中的第一条规则:

RewriteEngine On

RewriteCond %{REQUEST_URI} ^(.*/)?product-x(/.*)?$ [NC]
RewriteRule ^ %1productx%2 [L,R=301,NE]