Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Gordon Saxby 1347 posts 1608 karma points
    Aug 02, 2015 @ 10:10
    Gordon Saxby
    0

    Help with Regex needed

    I have a site where there are a load of old URLs like

    /section/product/p-69-325/
    

    and I want to change them all to

    /section/product/
    

    Can I do that with the Regex option?

    Actually, the last part is always in the same format "p-" then a number, a "-" and finally a number - so can I detect that, remove it and redirect to the resulting link?

  • Nicholas Westby 2005 posts 6843 karma points c-trib
    Aug 02, 2015 @ 18:57
    Nicholas Westby
    0

    Perhaps this would work:

    ^/section/product/p-[0-9]+-[0-9]+/$
    

    Then you just use "/section/product/" as your destination URL.

    Here is how that regex works:

    • ^ matches the beginning of the string.
    • $ matches the end of the string.
    • [0-9]+ matches one or more digits.
  • Gordon Saxby 1347 posts 1608 karma points
    Aug 02, 2015 @ 19:03
    Gordon Saxby
    0

    Sorry, I think my message probably wasn't very clear!

    The "section" and "product" parts are variable, not those actual words. So it might be:

    /Cats/ginger/p-01-123/
    /Cats/black/p-04-342/
    /Dogs/white/p-21-334/
    

    exc.

    I want to drop the last section and redirect to the first 2 sections (e.g. /cats/ginger/). I suspect this tool won't allow me to do that - maybe I need to do it in IIS?

  • Nicholas Westby 2005 posts 6843 karma points c-trib
    Aug 02, 2015 @ 20:37
    Nicholas Westby
    0

    Not sure about 301 URL Tracker in particular, but in general that is possible using named capture groups. So, the regex would look like this:

    ^/(?<SECTION>((?!/).)+)/(?<PRODUCT>((?!/).)+)/p-[0-9]+-[0-9]+/$
    

    And the replacement would look like this:

    /${SECTION}/${PRODUCT}/
    

    This new regex also makes use of what is called a "negative lookahead". That ensures the "SECTION" and "PRODUCT" groups don't contain a forward slash.

    Give it a try and let us know how it goes.

  • Nicholas Westby 2005 posts 6843 karma points c-trib
    Aug 06, 2015 @ 04:24
    Nicholas Westby
    0

    Did you ever get this working, Gordon?

  • Gordon Saxby 1347 posts 1608 karma points
    Aug 20, 2015 @ 17:14
    Gordon Saxby
    0

    Sorry for the delay ... been on holiday!

    I ended up doing it in IIS with Url Rewrite.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies