简体   繁体   中英

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.

How can I get the number entered (/d) to the second part of the link (instead of the 0)?

Thanks for your help and time

You should add the \\d+ (1 or more digits) to the capturing group:

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

And replace using the $1 back-reference:

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

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