WordPress.org

Support

Support » Plugins and Hacks » Redirection » [Resolved] matching identical slugs

[Resolved] matching identical slugs

  • 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?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author John Godley

    @johnny5

    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.

    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.

    Plugin Author John Godley

    @johnny5

    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.

    It’s different URLs all the time following the same pattern… so it’s ^/.*/.* or not? 🙂

    Plugin Author John Godley

    @johnny5

    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.

    Thanks John!

    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

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘[Resolved] matching identical slugs’ is closed to new replies.
Skip to toolbar