duanpao9781
2017-11-10 12:56
浏览 55
已采纳

RegEx for URL模式,重新启动后重定向

I have been struggling and testing for the last two hours and simply cannot wrap my head around the whole RegEx-stuff enough in order to find a proper solution to this...

I am trying to redirect a couple of URLs from our old site to the new one due to a recent re-launch.

This is the current state of things / a demo of my RegEx

Essentially it looks like this:

.+(\/es|\/de|\/en)?\/(legal)(.+)?

My problem is that a URL like https://example.com/es/projects/legal-yeah is also being matched, which does make sense looking at the rule but is not what I want to achieve...

How can I perform a test which only matches URLs where there is nothing in between the first part for the language string (de/en/es/empty) and the second part (/legal)?

Thanks so much for sharing your thoughts on this, appreciate it!

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douzhao7445 2017-11-10 15:34
    已采纳

    This final RegEx-rule matches the URLs like I intended – ignoring any other occurences of the "legal"-string (in this case) which might appear in another URL on some other level and 'fuzzy' enough to include all the language-cases, even without a language-string appearing at all.

    Solution

    The trick in the end was to force the rule to look for a TLD in front of the other stuff so it would only allow for first-level URLs to be included.

    UPDATE: My first solution didn't turn out to work since the redirection engine / plugin only makes use of the URL path, not including the domain (see GitHub issue) and as such I can't match the DOT as needed precessor.

    Now the rule is paying attention to the start of the string and not accepting anything other the language string in front of the targeted URL-slug which in turn removes false positives.

    Thanks to @Xatenev who pointed me in the right direction!

    已采纳该答案
    打赏 评论
  • dousilie9522 2017-11-10 13:12

    By using an end-of-line anchor $ and explicitly adding (\/.*) after legal you can achieve what you need:

    .+(\/es|\/de|\/en)?\/(legal)(\/.+)?$
    

    https://regex101.com/r/HsIDkQ/8

    打赏 评论

相关推荐 更多相似问题