admin管理员组文章数量:1130349
I have the following 2 redirects active:
/code-examples/(.*)
-> /tutorials/android/$1/
and
/code-examples/android/(.*)
-> /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?
I have the following 2 redirects active:
/code-examples/(.*)
-> /tutorials/android/$1/
and
/code-examples/android/(.*)
-> /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?
Share Improve this question asked Oct 25, 2018 at 9:06 Florian WaltherFlorian Walther 1591 silver badge8 bronze badges 5 |1 Answer
Reset to default 1The problem with your RegEx patterns:
/code-examples/(.*) /code-examples/android/(.*)is that they only match when the ending slash (
/) is present in the URL; e.g.:# Example 1: $1 is 'page1' /code-examples/page1 /code-examples/android/page1 # Example 2: $1 is '' (empty) /code-examples/ /code-examples/android/ # Example 3 # No matches because the ending / is not present. /code-examples /code-examples/androidwhere the "placeholder" (i.e.
(.*)) matches thepage1in the first example, and''(i.e. empty string) in the second example.To make these work:
/code-examples /code-examples/androidyou can use
(?:/?(.*)|\b)instead of/(.*), like so:/code-examples(?:/?(.*)|\b) /code-examples/android(?:/?(.*)|\b)But since you redirect to the same URL (
/tutorials/android/$1/), you can combine those RegEx patterns like so:/code-examples(?:/android|)(?:/?(.*)|\b)where
/code-examples(?:/android|)matches either/code-examplesor/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.
Resources:
Redirect Regular Expressions: https://redirection.me/support/redirect-regular-expressions/
https://regexr/41vch — for testing the combined RegEx pattern. But note that on RegExr, you need to use
\/and not just/. I.e. escape the/with a\.
I have the following 2 redirects active:
/code-examples/(.*)
-> /tutorials/android/$1/
and
/code-examples/android/(.*)
-> /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?
I have the following 2 redirects active:
/code-examples/(.*)
-> /tutorials/android/$1/
and
/code-examples/android/(.*)
-> /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?
Share Improve this question asked Oct 25, 2018 at 9:06 Florian WaltherFlorian Walther 1591 silver badge8 bronze badges 5- Where are these redirects? .htaccess? – Jacob Peattie Commented Oct 25, 2018 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 Commented Oct 25, 2018 at 9:34 - Thank you! Will the regex you posted solve my problem? Looks hella complicated – Florian Walther Commented Oct 25, 2018 at 21:19
- And I added the redirects with the Redirection plugin. I am a noob. – Florian Walther Commented Oct 25, 2018 at 21:19
- @FlorianWalther Yes, it will. Check my answer and let me know. – Sally CJ Commented Oct 26, 2018 at 3:13
1 Answer
Reset to default 1The problem with your RegEx patterns:
/code-examples/(.*) /code-examples/android/(.*)is that they only match when the ending slash (
/) is present in the URL; e.g.:# Example 1: $1 is 'page1' /code-examples/page1 /code-examples/android/page1 # Example 2: $1 is '' (empty) /code-examples/ /code-examples/android/ # Example 3 # No matches because the ending / is not present. /code-examples /code-examples/androidwhere the "placeholder" (i.e.
(.*)) matches thepage1in the first example, and''(i.e. empty string) in the second example.To make these work:
/code-examples /code-examples/androidyou can use
(?:/?(.*)|\b)instead of/(.*), like so:/code-examples(?:/?(.*)|\b) /code-examples/android(?:/?(.*)|\b)But since you redirect to the same URL (
/tutorials/android/$1/), you can combine those RegEx patterns like so:/code-examples(?:/android|)(?:/?(.*)|\b)where
/code-examples(?:/android|)matches either/code-examplesor/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.
Resources:
Redirect Regular Expressions: https://redirection.me/support/redirect-regular-expressions/
https://regexr/41vch — for testing the combined RegEx pattern. But note that on RegExr, you need to use
\/and not just/. I.e. escape the/with a\.
本文标签: Redirect regex misbehaving when placeholder empty
版权声明:本文标题:Redirect regex misbehaving when placeholder empty 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749231645a2336587.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


/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 Commented Oct 25, 2018 at 9:34