nix, shell, perl, php, mysql and mac os x tips and tricks

Monday, July 18, 2011

Remove multiple instances of a character (or a pattern) from a URL, then do a 301 redirect, with mod_rewrite

There was actually some html tags appearing in some of my URLs. This hack strips tags and does a search-engine-friendly redirect.
RewriteCond %{REQUEST_URI} ^(.*)<.*?>(.*)$
RewriteRule ^.*$ %1%2 [E=replacer:%1%2]
RewriteCond %{ENV:replacer} !^$
RewriteCond %{ENV:replacer} !^.*<.*?>.*$
RewriteRule ^.*$ %{ENV:replacer} [R=301,L]

This part matches the tags: "<.*?>". Replace that with your character or pattern.

No comments:

Post a Comment