How is regex translated?

#83321
  • Resolved David Graham
    Rank Math free

    Is there a reference to how RankMath translates redirections with the ‘Regex’ option?

    It’s not working as expected. I have tried all these combinations with and without escaping, with and without delimeters, etc. If you could tell me the correct way to enter regex for best results, I can then test more reliably. I’ve been trying to troubleshoot by exporting to htaccess to see the generated regex, which seems to be translated differently depending if you choose Regex or one of the other options – Exact, Contains, Starts With, Ends With – vs Regex.

    I’m having the following issues:
    URLs with hyphens - don’t seem to work when ‘Regex’ is selected. Probably because - is a special character that needs to be escaped; however, if I add a backslash \-, it gets double escaped as \\- and the hyphen still doesn’t match. Hyphens are properly escaped as \- if I use the ‘Contains’ option.

    Example
    From: ^/knowledge-centre/reports(.*)$ saved in htaccess as knowledge-centre/reports(.*)
    To: /reports$1
    Test: /knowledge-centre/reports/report-name-here
    No match – goes to default page

    Entering #^/knowledge\-centre/reports(.*)$# saves to htaccess as ^/knowledge\\-centre/reports(.*)$
    also no match
    Entering ‘Starts With’ /knowledge-centre/reports saves to htaccess as knowledge\-centre/reports
    which does match, but captures don’t seem to work.

    String start/end anchors ^$ are stripped out in the generated htaccess unless pattern delimiters are added. Does this mean anchors are ignored when processing the regex? If the matching is using PHP preg_match, should I include delimiters for more reliable results?

    See above examples.

    Captures – do captures work for Exact, Contains, etc, or only for Regex type?
    When using regex, captures work unexpectedly – seem not to capture trailing slashes or empty string; if the capture is empty the capture expression is passed as a literal which breaks the URL and redirects to the default.

    From: ^/news/press-releases(.*)$
    To: /press-releases$1
    seems to mostly work (which is weird because it has a hyphen)
    Matches /news/press-releases/article-name-here
    Does NOT match /news/press-releases/ or /news/press-releases
    Expected: $1 should capture the trailing slash or empty string and append to the new URL so /news/press-releases/ goes to /press-releases/ and /news/press-releases goes to /press-releases

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hiya.

    Just checking if this question is in a queue and I should wait for an answer, or go about solving this in a different way?

    I love RankMath so far and it would be convenient to have everything in one place, but if this won’t work, I can process legacy redirects separately if necessary.

    Thanks!

    Hello,

    Thank you for your query and we are so sorry about the trouble this must have caused. We apologize for the delay in our response.

    We are overwhelmed right now due to being shorthanded but I have shared your ticket with the dev. team and you will receive a reply very soon.

    Sorry about the delay, once again.

    I understand. No problem and thanks for the reply. Looking forward to their thoughts.

    • This reply was modified 3 years, 7 months ago by David Graham.

    Hello,

    1. URLs with hyphens:
    Please try adding the following redirection rule to redirect the reports page:
    Source ^knowledge-centre/reports/(.*)
    Destination: /$1

    This redirection rule will redirect http://example.com/knowledge-center/reports/report-page to http://example.com/report-page. If you want to redirect page to reports/report-page the please change the source to ^knowledge-centre/(.*)

    2. String start/end anchors
    Yes, we use preg_math to check the regex redirection rule. Are you trying to create a redirection rule in the htaccess file or are the redirections not working after exporting the Redirection rules to htaccess from Redirection settings?

    3. Captures
    Expected: $1 should capture the trailing slash or empty string and append to the new URL so /news/press-releases/ goes to /press-releases/ and /news/press-releases goes to /press-releases

    I was able to reproduce this issue and have logged it. Please allow us some time to test and release a fix

    In the meantime, if you need any other assistance, please feel free to let us know. We are here to help.

    Thank you.

    Thanks Pratik for the reply.

    On 2. – I was using regex within RankMath, but when it failed, it was difficult to troubleshoot, so I tried exporting to htaccess to look at how the regex was saved/escaped.

    I think point 3. was the core issue. When the capture failed on trailing slashes/empty strings, I tried to find a way to make it work by adding manual delimiters etc and got very confused.

    It sounds as if the process is stripping away start and end anchors and then leaving forward slash as the default delimiter, so ^/knowledge-centre/reports/$ would become /knowledge-centre/reports/ which preg_match reads as knowledge-centre/reports. Does that sound right?

    I’ll see if I can find a workaround for trailing slashes e.g. having two separate rules to redirect the match with capture and exact match without, like:
    Source knowledge-centre/reports (exact match?)
    Destination: /
    Source ^knowledge-centre/reports/(.*) (regex)
    Destination: /$1

    I’ll report back on whether this works.

    • This reply was modified 3 years, 7 months ago by David Graham.
    • This reply was modified 3 years, 7 months ago by David Graham.

    UPDATE:
    Exact match seems to ignore trailing slashes so it looks like I can work around the empty capture issue by having a pair of rules – an exact match for the ‘directory’ and a Regex capture for when there’s something at the end.

    I’ll continue to test.

    Hello,

    On 2. – I was using regex within RankMath, but when it failed, it was difficult to troubleshoot, so I tried exporting to htaccess to look at how the regex was saved/escaped.

    Please try adding the regex rule without $ at the end i.e
    Source: ^knowledge-centre/reports/(.*)
    Destination: /$1

    This should fix your regex redirection issue. And the trailing slash issue which you mentioned is the bug in our plugin and I have logged it. You’ll see the fix for that soon in one of the upcoming updates.

    In the meantime, please let us know if you need our help with anything else.

    Thank you.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

Viewing 7 replies - 1 through 7 (of 7 total)

The ticket ‘How is regex translated?’ is closed to new replies.