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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?