• Resolved webdesigner72

    (@webdesigner72)


    I’m trying to create a regular expression rule for the source url based on the following pattern:

    part1/dynamic/part2/part3

    I understand how to write a regular expression for when the dynamic part of the expression is at the beginning (^/part2/part3) or end (part1/.*), but not in the middle as per above.

    How could I do this?

Viewing 5 replies - 1 through 5 (of 5 total)
  • CEMBTW

    (@cembtw)

    You want to keep that dynamic part in the destination, or just want it accounted for and caught by the regex?

    Thread Starter webdesigner72

    (@webdesigner72)

    The dynamic part will be a username. I am trying to create redirects for some Buddypress urls such as:
    members/buddypresstester/settings/notifications/
    part1/dynamic/part2/part3

    Since the username part will always be different based on which user’s profile is being viewed, I would like to write a redirect rule as a regular expression to cater for this dynamic part.

    CEMBTW

    (@cembtw)

    Regex redirects have capture groups that can be passed to the desired destination.

    So a structure like: /oldmembers/myusername/oldposts/ could have a regex like: ^\/oldmembers\/(.*)\/oldposts\/$.

    That part in parentheses (now stored as $1—subsequent groups would be $2, $3, etc.) would be called in your destination like: /newmembers/$1/newposts/.

    Click the ^ arrow next to “Substitute” here to test them out. https://regex101.com

    • This reply was modified 7 years ago by CEMBTW.
    • This reply was modified 7 years ago by CEMBTW.
    Thread Starter webdesigner72

    (@webdesigner72)

    Thank you for that, it works almost perfectly. When I click on a link that would take me to the redirect path (members/buddypresstester/settings/notifications/) I am redirected correctly. But if I copy and paste this address into the address bar and hit enter, for some reason the redirect does not happen. Is there a setting that can change this so it doesn’t happen?

    Also, how would I set the target url to the home page or an external url? When I type https://www.google.com into the target url this is simply appended onto the address where I currently am. Apologies if these are very basic questions.

    Plugin Author John Godley

    (@johnny5)

    There’s no difference between clicking a link and typing it, so it’s likely that the URL you’re typing is different from the one you are clicking.

    You can just enter the external URL into the target field. You’ll need to make sure nothing else is in the target

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Regular expression with variable in the middle’ is closed to new replies.