Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

The backend URL would need to look like the following

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
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:


$| = 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


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.