RewriteRule 替换 URL 中的子字符串

RewriteRule to Replace Substring in URL

我的问题听起来很简单,但是我想不通,到处找遍了,我什么也没找到。

我有多个具有以下名称格式的页面:

www.mydomain.com/cat1/name-prefix-***hello-world***-name-suffix.html

我需要创建一个输出以下名称格式的规则:

www.mydomain.com/cat1/name-prefix-***new-static-string***-name-suffix.html

我想,因为我有多个页面,所以我必须使用正则表达式来匹配子字符串 hello-world 和将其替换为 new-static-string 但我不确定这是否可以通过 mod_rewrite 实现或者我是否应该采取不同的方法。

此外,我的页面并不完全位于目录 /cat1 中。它们可以在 /cat2/cat3/whatever_cat 中,也可以在根目录或第五级子目录中。

换句话说,无论文件路径如何,规则都必须只针对文件名。

规则很简单:

RewriteEngine On
RewriteRule ^(.*)-hello-world-(.*)$ -new-static-string- [R=301,L,NC]