模式不适用于重写规则

问题描述:

我有2种形式的相同的URL(只有差异是?之前的最后一个斜杠),并且我需要创建单个模式(正则表达式)。所以相同的模式可以处理这两种URL的变体。模式不适用于重写规则

  • products/dispenser/hand-towel?gclid=CPDv
  • products/dispenser/hand-towel/?gclid=CPDv

我尝试使用下面的图案,这是工作的罚款,第一次URL,但不工作的第二个URL。

  • ^products/([^/]+)/([^/]+)/?$

我想更多一些下面的模式,但没有获得成功。

  • ^products/([^/]+)/([^/]+/.*| /?$)
  • ^products/([^/]+)/([^/]+)(^/?)$
  • ^products/([^/]+)/([^/]+)/?$

通过使用相同的模式我想下面的输出,这我穿过第一URL获得。

  • {R:1} Dispenser
  • {R:2} hand-towel?gclid=CPDv

,并从第二URL我需要得到下面的输出

  • {R:1} Dispenser
  • {R:2} hand-towel/?gclid=CPDv

我有6种网址

  1. products/dispenser/hand-towel?gclid=CPDv
  2. products/dispenser/hand-towel/?gclid=CPDv
  3. products/dispenser/hand-towel

  4. products/dispenser/hand-towel/hand-roll?gclid=CPDv

  5. products/dispenser/hand-towel/hand-roll/?gclid=CPDv
  6. products/dispenser/hand-towel/hand-roll

因此,所有上面这些URL将登陆同一个页面上,但如果前3个网址将被打,然后我需要dispenserHand-towel为R1和R2,如果第4,第5和第6网址将被然后打我需要dispenserhand-towelhand-towel-roll作为R1,R2和R3。

^products/([^/]+)/([^/]+)/?$^products/([^/]+)/([^/]+)/?$模式仅适用于1和2个URL,因为此模式无法识别URL 4,5和R3的R3参数。

^products\/([^\/]+)\/([^\/]+)\/(.+)$模式不适用于第1个网址。

+0

@Fallen英雄:谢谢你的努力。 但输出中仍缺少一些部分,我更新了我的问题以获得更好的说明,请参阅。 – user3305540

你的问题是$在最后。这意味着end of line但你有?gclid=CPDv你仍然想匹配。

在这里看到:https://regex101.com/r/SkHXWH/2

拍摄的组会给你线索。

你为什么不只是这样做:

^products\/([^\/]+)\/(.+)$ 
      ^^^R1^^ ^R2^ 

这会给你想要的输出/捕获组

您提供的网址在评论

^products\/([^\/]+)\/([^\/]+)\/(.+)$ 
      ^^^R1^^^ ^^^R2^^^ ^R3^ 

或声明只有2组

^products\/([^\/]+)\/(?:[^\/]+)\/(.+)$ 
      ^^^R1^^^    ^R2^ 
+0

它看起来对于两个URL都有效,但是这里给出的部分是{R:1}和{R:2},因为我需要使用这些r1和r2来创建新的URL。 – user3305540

+0

我需要在Pattern中为这个URL添加什么。产品/分配器/手毛巾/手毛巾卷/?GCLID = CPDv – user3305540