0

I need some assitance with a mod_rewrite rule.

I am setting up a SEO Friendly director of names and have run into some problems I know there is an easy answer for but I cant get it to work to save my life.

Here is a sample URL's that we would like to publish

http://www.foo.com/bar/ca/marina-del-ray/first-last.html

The backend URL would need to look like the following
/bar/?searchState=ca&searchCity=marina+del+ray&firstName=first&lastName=last

As you can see for the city I need to replace the "-" with "+"

Any assitance would be greatly appriciated, I would like to be able to make this rule dynamic enough that if a city name contains 1 or 15 words it will still tranfer over without having to create 15 seperate rules for each permiation.

Many thanks for any input or solutions.

1 Answers1

1

At the risk of sounding like a broken record (because I offered a similar solution here), this sounds like another situation in which a programmatic RewriteMap would work well.

In your Apache configuration, something like:

RewriteMap rewriter prog:/path/to/script
RewriteRule ^(.*) %{rewriter:$1} [L]

And in your script, something like:

#!/perl 

$| = 1; 

# /bar/ca/marina-del-ray/first-last.html
while (<STDIN>) { 
        my @parts = split /\//;
        my @name = split /-/, $parts[4];
        $name[1] =~ s/\.html$//;
        grep { s/-/+/g } @parts;

        print "/$parts[1]/?searchState=$parts[2]&searchCity=$parts[3]&",
                "firstName=$name[0]&lastName=$name[1]", "\n";
}

This will perform exactly the URL transformation you've requested. Please, please note that this is just example code -- a real solution would need to be much more robust.

larsks
  • 41,276
  • 13
  • 117
  • 170
  • Many thanks this worked great I somehow missed the whole RewriteMap documentation let alone running a program for the map. – Gary Steven Dec 18 '09 at 07:57