DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 5.11 Replacing Text in Requested URLs

Problem

You want to change all occurrences of string1 to string2 in a request's URI.

Solution

RewriteCond %{REQUEST_URI} "string1"
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.

See Also

    [ Team LiB ] Previous Section Next Section