简体   繁体   English

带数字的正则表达式重定向

[英]Regex Redirection with numbers

I wish to redirect a link from我想重定向一个链接

http:www.testurl.com/1209/somecategory/itemid

to

http:www.testurl.com/en/uk/1209/somecategory/itemid

by using a regex expression.通过使用正则表达式。

At the moment I have come up with目前我想出了

<redirect url="~/\d/(.*)$" to="~/en/uk/0/$1/" />

which works when the number is 0.当数字为 0 时有效。

How can I get the number entered (/d) to the second part of the link (instead of the 0)?如何将输入的数字 (/d) 输入到链接的第二部分(而不是 0)?

Thanks for your help and time感谢您的帮助和时间

You should add the \\d+ (1 or more digits) to the capturing group:您应该将\\d+ (1 位或更多位)添加到捕获组:

url="~/(\d+/.*)$"

And replace using the $1 back-reference:并使用$1反向引用替换:

to="~/en/uk/$1/"

Thus, you do not have to hard-code digits into the replacement string.因此,您不必将数字硬编码到替换字符串中。

See Use Parentheses for Grouping and Capturing and Numbered Backreferences for more details on how capturing can be used in regex.有关如何在正则表达式中使用捕获的更多详细信息,请参阅使用括号进行分组和捕获以及编号反向引用

See Regex Demo请参阅正则表达式演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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