I have the following 2 redirects active:

-> /tutorials/android/$1/


-> /tutorials/android/$1/

The first one has a higher position, so the second will only be executed if the first one didn't apply. This works if there is something in the placeholders, i.e. /code-examples/page1 leads to /tutorials/android/page1.

The problems occur when the placeholders are empty. Just /code-examples or /code-examples/android WITHOUT the trailing slash leads to 404. With the trailing slash, I can see that it adds a second slash at the end, which results in /tutorials/android//.

How can I get the redirects to work properly with and without something in the placeholders?

  • Where are these redirects? .htaccess? – Jacob Peattie Oct 25 '18 at 9:22
  • 2
    Your code can be combined: /code-examples(?:/android|)(?:/?(.*)|\b) And the problem with your REGEX is because it's "requiring" the / (slash) at the end. Btw, this question would have probably been better asked on Stack Overflow. – Sally CJ Oct 25 '18 at 9:34
  • Thank you! Will the regex you posted solve my problem? Looks hella complicated – Florian Walther Oct 25 '18 at 21:19
  • And I added the redirects with the Redirection plugin. I am a noob. – Florian Walther Oct 25 '18 at 21:19
  • @FlorianWalther Yes, it will. Check my answer and let me know. – Sally CJ Oct 26 '18 at 3:13
  • The problem with your RegEx patterns:


    is that they only match when the ending slash (/) is present in the URL; e.g.:

    # Example 1: $1 is 'page1'
    # Example 2: $1 is '' (empty)
    # Example 3
    # No matches because the ending / is not present.

    where the "placeholder" (i.e. (.*)) matches the page1 in the first example, and '' (i.e. empty string) in the second example.

  • To make these work:


    you can use (?:/?(.*)|\b) instead of /(.*), like so:

  • But since you redirect to the same URL (/tutorials/android/$1/), you can combine those RegEx patterns like so:


    where /code-examples(?:/android|) matches either /code-examples or /code-examples/android.

I added the redirects with the Redirection plugin.

I suppose you have something like this on the Tools → Redirection → Redirects page (in wp-admin):

Now with the combined RegEx pattern, delete the second Redirect and edit the first one like so:

Note: For the Type and Group settings, just use your existing setup.


| improve this answer | |
  • You're welcome, I'm glad it helped you. :) – Sally CJ Oct 26 '18 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.