WordPress.org

Ready to get started?Download WordPress

Forums

Redirection
[resolved] matching identical slugs (8 posts)

  1. titanas
    Member
    Posted 3 years ago #

    Hey,

    How to redirect something like /ab/ab/ab/ab => ab ?

    ab is actually a WordPress slug something like /wordpress-is-awesome/ so the actual problematic slug looks like this: /wordpress-is-awesome/wordpress-is-awesome/wordpress-is-awesome/wordpress-is-awesome/ and want to be redicted to /wordpress-is-awesome/ only

    A solution like (.*)/(.*)/(.*)/(.*) => $1 dosen't make sense because it conflicts with stuff like /tag/help/page/3

    Any ideas?

  2. John Godley
    Member
    Plugin Author

    Posted 3 years ago #

    You'll need to be more specific with your regex so it only matches the exact URL you want to redirect. Also, you can use ^ and $ to match only the start and end of a URL.

  3. titanas
    Member
    Posted 3 years ago #

    Thank for the reply.. that's the thing, i don't know how to be more specific. Can you help in an example like /wordpress-is-awesome/wordpress-is-awesome/wordpress-is-awesome/wordpress-is-awesome/ ?

    I'll play with ^ and $ and see what happens.

  4. John Godley
    Member
    Plugin Author

    Posted 3 years ago #

    Something like this would not match any other URLs:

    ^/wordpress-is-awesome/.*

    If you only have the one URL to match then there is no need to use a regex - just match the entire URL.

  5. titanas
    Member
    Posted 3 years ago #

    It's different URLs all the time following the same pattern... so it's ^/.*/.* or not? :)

  6. John Godley
    Member
    Plugin Author

    Posted 3 years ago #

    Then it looks like unless you can find a way to uniquely describe the URLs there is no single pattern that will fit. A regex is powerful but it won't fit all situations.

  7. titanas
    Member
    Posted 3 years ago #

    Thanks John!

  8. jrf
    Member
    Posted 3 years ago #

    Titanas,

    There is actually a way to do exactly what you want with regular expressions, though by the looks of the above thread, it is a lesser known feature:

    Source URL: ^(/[a-z0-9-]+){\1}+[/]?$
    Target URL: $1

    Explanation:
    () the regex remembers what is between brackets. With \1 you have the regex recall the remembered string and match that again one or more times - in this case '/wordpress-is-awesome' -. I put the \1 between {} to avoid the regex confusing it with a literal pattern.

    In the target use $1 as the starting / is already included in the match.

    Hope this helps!

    Smile,
    Juliette

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic