Search code examples
regexwordpressurl-rewritingurl-routing

Regular expression to match string from url


I want to match shop name from a url .Please see the example below. Its for url redirection in a word press application.

See the examples given below

http://example.com/outlets/19-awok?page=2
http://example.com/outlets/19-awok
http://example.com/outlets/159-awok?page=3

In all cases i need to get only awok from the url .It will be the text coming after '-' and before query string .

I tried below and its not working

/outlets/(\d+)-(.*)? => /shop/$2

Any help will be greatly appreciated.


Solution

  • You can use this regex:

    /outlets/\d+-([^?]+)?
    

    Trailing ? is used to strip previous query string.