Cubik Rube
Cubik Rube

Reputation: 23

RewriteRule to Replace Substring in URL

My problem sounds very simple, however I can’t figure it out and after I looked everywhere as much as could, I didn't find anything.

I have multiple pages that have the following name format:

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

I need to create a rule that would output the following name format:

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

I guess, since I have multiple pages I have to use regular expression that would match the substring hello-world and replace it with new-static-string but I am not sure if this can be achieved with mod_rewrite or if I should take a different approach.

Also my pages are not exclusively in the directory /cat1. They can be in /cat2, /cat3 or /whatever_cat and also they can be in the root or at a fifth level sub-directory.

In other words the rule will have to target the file name only regardless the file path.

Upvotes: 2

Views: 3728

Answers (1)

hjpotter92
hjpotter92

Reputation: 80653

The rule is as simple as:

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

Upvotes: 6

Related Questions