5.11. Replacing Text in Requested URLs
Problem
You want to change all occurrences of
string1 to
string2 in a request’s URL.
Solution
RewriteRule "(.*)string1(.*)" "$1string2$2" [N,PT]
Discussion
The [N] flag tells Apache
to rerun the rewrite rule. This rule will get run repeatedly until the
RewriteCond fails. Thus, it will
get rerun as long as the URL contains the string that you want to
replace. As soon as all occurrences of this string have been replaced,
the RewriteCond will fail, and the
rule will stop. The [PT] tells
mod_rewrite to pass the rewritten
URL on to the rest of Apache for any additional processing once the
rewriting is done.
Care should be taken to avoid infinite loops, such as might
happen if string1 is part of
string2.
See Also
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access