使用Tuckey的URL重写过滤器向URL添加尾斜杠

时间:2021-08-06 10:45:25

I'd like to be able to add a trailing slash to URLs that are missing it (before the parameters). This should be done using this filter : http://www.tuckey.org/urlrewrite/

我希望能够向丢失它的url添加一个尾斜杠(在参数之前)。这应该使用这个过滤器完成:http://www.tuckey.org/urlrewrite/

In other words:

换句话说:

  • /web/guest should become /web/guest/
  • / web /客人应该成为/ web /客人
  • /web/guest? should become /web/guest/?
  • / web /客人吗?应该成为/ web /客户/ ?
  • /web/guest/? should not be altered
  • / web /客户/ ?不应该改变
  • /web/guest?test=1 should become /web/guest/?test=1
  • / web /客人吗?测试= 1应该成为/ web /客人/ ?测试= 1
  • /web/guest/anything/ should not be altered
  • /网站/客人/任何东西/不应该被改变

That is easy when the URL has no parameters, but I can't find a matching regex when there are some.

当URL没有参数时,这很容易,但是当有参数时,我找不到匹配的regex。

Here's what I could come up with (with exemples and unit tests): https://regex101.com/r/qU3rP3/2:

下面是我可以想到的(包括示例和单元测试):https://regex101.com/r/qU3rP3/2:

^\/(.+\/[^\/]+?)(\?.*)?$

and replacing with /$1/$2. It does not yield the expected results.

和替换/ $ 1 / 2美元。它不会产生预期的结果。

Thanks for your help

谢谢你的帮助

1 个解决方案

#1


2  

You can use

您可以使用

([^/?])$|/?([?].*)

Replace with $1/$2.

替换为1 / 2美元。

See regex demo.

查看演示正则表达式。

The pattern contains two alternatives and captures them into Group 1 and 2. Then, in the replacement pattern, we refer to these captured values with $1 and $2 backreferences.

该模式包含两个备选方案,并将它们捕获到第1和第2组。然后,在替换模式中,我们使用$1和$2反向引用来引用这些捕获的值。

Details:

细节:

  • ([^/?])$ - match and capture into Group 1 any character but a / or ? at the end of the string
  • ([^ / ?])——匹配和捕捉到组1美元任何字符,但/或?在弦的末端
  • | - or
  • |——或者
  • /? - an optional (1 or 0) forward slash
  • / ?-可选的(1或0)正斜线
  • ([?].+) - match and capture into Group 2 a literal ? followed with 1+ characters other than a newline
  • ([?].+) -匹配并捕获到第二组文字?除了换行符之外,后面还有1+个字符

The replacement works even in case one capture group is empty because a non-participating capture group is filled with an empty string after a match.

即使在一个捕获组为空的情况下,替换仍然有效,因为非参与捕获组在匹配后被空字符串填充。

#1


2  

You can use

您可以使用

([^/?])$|/?([?].*)

Replace with $1/$2.

替换为1 / 2美元。

See regex demo.

查看演示正则表达式。

The pattern contains two alternatives and captures them into Group 1 and 2. Then, in the replacement pattern, we refer to these captured values with $1 and $2 backreferences.

该模式包含两个备选方案,并将它们捕获到第1和第2组。然后,在替换模式中,我们使用$1和$2反向引用来引用这些捕获的值。

Details:

细节:

  • ([^/?])$ - match and capture into Group 1 any character but a / or ? at the end of the string
  • ([^ / ?])——匹配和捕捉到组1美元任何字符,但/或?在弦的末端
  • | - or
  • |——或者
  • /? - an optional (1 or 0) forward slash
  • / ?-可选的(1或0)正斜线
  • ([?].+) - match and capture into Group 2 a literal ? followed with 1+ characters other than a newline
  • ([?].+) -匹配并捕获到第二组文字?除了换行符之外,后面还有1+个字符

The replacement works even in case one capture group is empty because a non-participating capture group is filled with an empty string after a match.

即使在一个捕获组为空的情况下,替换仍然有效,因为非参与捕获组在匹配后被空字符串填充。