Need a mod_rewrite .htaccess solution to replace %20 spaces with -'s in the finished URL

不想你离开。 提交于 2019-12-11 07:15:30

问题


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:


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).



来源:https://stackoverflow.com/questions/10407594/need-a-mod-rewrite-htaccess-solution-to-replace-20-spaces-with-s-in-the-fini

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!