Need a mod_rewrite .htaccess solution to replace

2019-08-03 00:54发布

I need an .htaccess mod_rewrite solution that will take a .cgi search query like this:

www.mydomain.com/cgi-bin/finda/therapist.cgi?Therapy_Type=Pilates Training&City=Los Angeles&State=CA

and return matching results in the browser's address bar to look like this:

  • www.mydomain.com/therapists/Pilates-Training-Los-Angeles-CA.html

or better yet:

  • www.mydomain.com/therapists/pilates-training-los-angeles-ca.html

Notice the database includes values with one, two or three words + spaces...

For example:

Therapy_Type=Pilates Training <- includes a space

City=Los Angeles <- includes a space

State=CA <- no space

I used the tool at: http://www.generateit.net/mod-rewrite/ to generate the following RewriteRule:

RewriteEngine On
RewriteRule ^([^-]*)-([^-]*)-([^-]*)\.html$ /cgi-bin/finda/therapist.cgi?Therapy_Types=$1&City=$2&State=$3 [L]

This does work (finds the search matches) and generates the results page, but because the parameter values have spaces in them, we end up with a URL that looks like this:

www.mydomain.com/therapists/Pilates%20Training-Los%20Angeles-CA.html

I've spent days in this forum and others trying to find a solution to get rid of these %20 (encoded spaces) so the final returned URL will look like 1) or 2) above.

I know someone on here must know how to do this... Help ;-)

1条回答
Rolldiameter
2楼-- · 2019-08-03 01:46

If you replace the %20 with -, then how would you know where the therapy type ends and the city starts?

pilates-training-los-angeles-ca

would be

type=pilates
city=training
state=los

So I don't think you like to replace the %20 by -. You could however replace it with another character, like _:

pilates_training-los_angeles-ca

You then would have to translate every _ to a space within your PHP script (or whatever language you are using server side).

查看更多
登录 后发表回答