[Gllug] mod rewrite - regex replace
Joel Bernstein
joel at fysh.org
Sun Dec 7 18:03:19 UTC 2008
2008/12/7 Sean Burlington <sean at practicalweb.co.uk>:
> I have an old blog - I've moved some of the content to me newer site and
> want to redirect any traffic from the old site to the new site using the
> site search function
>
> So far I have:
>
> RewriteEngine On
> RewriteRule .*/(.*)/ http://www.practicalweb.co.uk/search/node/$1 [L]
>
> This works OK as far as it goes
>
> hits on
>
> http://www.uncertainty.org.uk/wordpress/2008/01/03/techie/version-control-and-drupal/
>
> end up at
>
> http://www.practicalweb.co.uk/search/node/version-control-and-drupal
>
> But I'd like to get to here
>
> http://www.practicalweb.co.uk/search/node/version+control+and+drupal
>
> Is there any way to change the url ?
>
> $1 = preg_replace('/-/\+/', $1); # in php speak
>
> Can I do this in mod_rewrite?
>
> If not I guess I can adapt my search page ..
It may be easier to adapt your search page, but you could abuse
mod_rewrite's RewriteMap directive:
RewriteEngine On
RewriteMap minus_to_plus prg:/path/to/minus_to_plus.pl
RewriteRule .*/(.*)/
http://www.practicalweb.co.uk/search/node/${minus_to_plus:$1} [L]
--minus_to_plus.pl:
#!/usr/bin/env perl
use strict;
use warnings;
$|++; # unbuffer STDOUT
while (defined(my $line = <>)) {
$line =~ s{[-]}{+}g;
}
Essentially a RewriteMap with the "prg" map-type allows filtering of
content through arbitrary scripts.
Naturally your filter script can be written in any language, so long
as it does not use buffered I/O. I would not think that this is a
tremendously efficient way to achieve what you want - each hit will
involve forking a new Perl interpreter to perform a regex
substitution, whose result will not be cached.
Thus I recommend you will get better results adding a simple
substitution to the path dispatcher in your new webapp. But if you
want to do what you originally asked, an example is above. I do not
believe that mod_rewrite / mod_proxy have any builtin
regex-substitution capability on the RHS of a RewriteRule. You could
perhaps chain rules with lots of individual captures /(?:([^-]+-)+/ or
similar and then substitute into /$1+$2+$3+$4..../ but that's really
ugly and fragile IMO.
Hope this helps,
/joel
--
Gllug mailing list - Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug
More information about the GLLUG
mailing list