我试图重写一些urls并通过一个正则表达式通过路径名提取它们的参数值。url可以有多个变体,s项、pg-n、排序顺序等等都可以是任意顺序的。除了类别,它总是在网站网址之后第一。
示例1: www.test.com/freebies/s-term/pg-2/
示例2: www.test.com/freebies/pg-2/
所有可能的组合
http://localhost:8888/pg-2/
http://localhost:8888/s-term/
http://localhost:8888/s-term/pg-2/
http://localhost:8888/s-term/pg-2/
http://localhost:8888/freebies/
http://localhost:8888/freebies/pg-2/
http://localhost:8888/freebies/s-term/
http://localhost:8888/freebies/s-term/pg-2/在任何一种情况下,我都希望得到第1组中的类别,第2组中的搜索项,第3组中的分页,第4组中的排序顺序。
这是我到目前为止尝试过的,但在替换区域,您可以看到它每次创建匹配时都会替换,我希望用一个新的url替换整个url,并包含组数据。
https://regex101.com/r/4v1uf5/10
你认为通过regex或.htaccess重写可能是一个更好的计划吗?
谢谢!
发布于 2020-05-03 11:53:49
最后,通过一个又一个地使用多个组来提取值来解决这个问题:
(?:(freebies)\/|pg-([^\/]+)\/|s-([^\/]+)\/)\/?(?:s-([^\/]+)\/|pg-([^\/]+)\/)?(?:pg-([^\/]+)\/)?https://stackoverflow.com/questions/61560658
复制相似问题