Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
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 7:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.