简体   繁体   中英

php clean urls using regex

i am trying to convert these urls

localhost/list/data/?search=keyword
localhost/list/data?search=keyword

to

localhost/list/data/search/keyword

i used this expression

(.+)(\?|\/\?)([a-z0-9_-]+)=([a-z0-9_-]+)

the problem is suppose that the user searches for another keyword

localhost/list/data/search/keyword/?search=anotherkeyword
localhost/list/data/search/keyword?search=anotherkeyword

it becomes like this

localhost/list/data/search/keyword/search/anotherkeyword

how can make it like this

localhost/list/data/search/anotherkeyword

where in it replaces the previous search/keyword in the url

Its a little more complicated than just regex. The best approach is a combination of server side scripting, and htaccess. I wrote up an answer for a very similar question not to long ago. As well as use this solution in a handful of sites I develop.

PHP dynamic DB page rewrite URL

Ill save the time from rewriting the post I made on the above link and just let you use that as your guide, hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM