正则表达式匹配不在我的域中的 URL

RegEx matching URLs that are NOT in my domain

我正在尝试使用重写策略设置我的 Netscaler 设备。我的要求之一是用主页 URL 替换任何非域 URL... 也就是说,我希望 Netscaler 替换从设备后面提供的页面上的所有外部链接使用主页的 URL(例如:https://my.domain.edu)。我尝试配置的重写策略类型使用符合 PCRE 的正则表达式引擎来查找网页上的特定文本(可能有多个匹配项)。

好的链接:

https://your.page.domain.edu -- won't be replaced  
http://good.domain.edu  -- also won't be replaced

错误链接(应替换为首页URL):

https://www.google.com    
http://not.the.best.example.org   
http://another.bad.example.erewhon.edu   
https://my.domain.com    

我目前有这个模式:

(https?://)(?![\w.-]+\.domain\.edu)

根据 Netscaler 的 RegEx 评估工具,这与上面的错误链接匹配,但与良好链接不匹配,因此它似乎有效...事实上,当我 运行 在测试中使用它时页面上,Netscaler 会找到我要替换的所有 URL,并单独留下好的 URL。

问题是 Netscaler 没有按照我想要的方式替换 URLs:它将 (https?://) 组替换为主页 URL 但保留其余部分部分不好URL。例如,它替换 http://www.google.com with: https://my.domain.eduwww.google.com

我可以配置重写策略来替换特定的 URL(例如,https://www.google.com),所以我知道该机制有效。显然,这不适用于一般情况。

我试过将整个正则表达式括在括号中,但这并没有改变任何东西。

是否可以为一般情况编写正则表达式,以匹配所有不属于我的域的整个 URL?

在此先感谢您的帮助!

您可以使用以下正则表达式:

^https?:\/\/[\w.-]+(?<!\.domain\.edu)$

用您的主页 URL 作为替代:

https://my.domain.edu

测试输入:

https://www.google.com
http://not.the.best.example.org
http://another.bad.example.erewhon.edu
https://my.domain.com
https://your.page.domain.edu
http://good.domain.edu

测试输出:

https://my.domain.edu
https://my.domain.edu
https://my.domain.edu
https://my.domain.edu
https://your.page.domain.edu
http://good.domain.edu

Demo on regex101

如果 http/https 比使用以下正则表达式更重要:

^(https?:\/\/)[\w.-]+(?<!\.domain\.edu)$

替换:

my.domain.edu

输入:

https://www.google.com
http://not.the.best.example.org
http://another.bad.example.erewhon.edu
https://my.domain.com
https://your.page.domain.edu
http://good.domain.edu

输出:

https://my.domain.edu
http://my.domain.edu
http://my.domain.edu
https://my.domain.edu
https://your.page.domain.edu
http://good.domain.edu

Demo2

查看原始 http 负载并确保链接与您认为的实际负载中的链接一致。

主机名通常是一个http头,协议通常不包含在页面内容等中。安装fiddler并观察原始数据。

Netscaler RegEx 按预期工作。

进一步:确保在尝试重写之前压缩任何压缩内容。如果不是,netscaler 将尝试将您的重写与压缩数据/分块内容相匹配。