简体   繁体   English

基于 PCRE 的用于重定向的正则表达式

[英]PCRE based Regex for redirection

I have some links as follow.我有一些链接如下。

http://www.example.com/excel.aspx
http://www.example.com:80/word.aspx
http://www.example.com:80/powerpoint.aspx
http://www.example.com:80/pdf.aspx
http://www.example.com:80/barcode.aspx
http://www.example.com/ocr.aspx
http://www.example.com/email.aspx
http://www.example.com/project.aspx

& I want to rewrite the URLs as following in a single regex pattern or single regex line. & 我想在单个正则表达式模式或单个正则表达式行中重写 URL,如下所示。

https://www.example.com/excel
https://www.example.com/word
https://www.example.com/powerpoint
https://www.example.com/pdf
https://www.example.com/barcode
https://www.example.com/ocr
https://www.example.com/outlook
https://www.example.com/project

If you check & compare, I have replaced email with outlook in substitution URL.如果您检查并比较,我已在替换 URL 中用 Outlook 替换了电子邮件。

I can match patterns as follow .*/(\\w+).aspx and substitute as https://www.example.com/ $1 but I cannot find a way to replace email with outlook .我可以匹配模式如下.*/(\\w+).aspx并替换为https://www.example.com/ $1但我找不到用Outlook替换电子邮件的方法。 Demo https://regex101.com/r/71huIx/24演示https://regex101.com/r/71huIx/24

Something similar to http://www.rexegg.com/regex-trick-conditional-replacement.html but without a pool or dictionary.类似于http://www.rexegg.com/regex-trick-conditional-replacement.html但没有池或字典。 However, if the pool or dictionary can be part of regex itself then it will OK.但是,如果池或字典可以是正则表达式本身的一部分,那么它就可以了。 Reason being, I have txt file with rules, where line1 is regex & line2 is a substitution.原因是,我有一个带有规则的 txt 文件,其中第 1 行是正则表达式,第 2 行是替换。

Any ideas how can I do that?任何想法我该怎么做?

Here's an Apache httpd config that should work:这是一个应该可以工作的 Apache httpd 配置:

RewriteEngine On
RewriteRule   ^/outlook$        https://example.com/email.aspx  [P,L]
RewriteRule   ^/([^./])/?$      https://example.com/$1.aspx     [P,L]

This uses a reverse proxy to serve pages like email.aspx and foobar.aspx when given "outlook" and "foobar" without revealing their extensions to users.当给定“outlook”和“foobar”时,它使用反向代理来提供诸如email.aspxfoobar.aspx类的email.aspx ,而不会向用户透露它们的扩展名。

Though it looks like you're trying to go backwards for some reason.虽然看起来你出于某种原因试图倒退。 In that case, you'd want:在这种情况下,你会想要:

RewriteEngine On
RewriteRule   ^/email\.aspx$    https://example.com/outlook     [L]
RewriteRule   ^/([^.]+)\.aspx$  https://example.com/$1          [L]

Regex conditional expressions cannot perform your task.正则表达式条件表达式无法执行您的任务。 The structure of a regex conditional is:正则表达式条件的结构是:

(?(condition)yes-pattern|no-pattern)
  • condition is either condition
    • a zero-width assertion (typically a lookahead)零宽度断言(通常是前瞻)
    • a parenthesized integer, denoting whether that numbered capture has content一个带括号的整数,表示该编号的捕获是否有内容
    • a capture group name in angle brackets or single quotes尖括号或单引号中的捕获组名称
    • (R) which is true when evaluated inside a recursion or eval (R)在递归或 eval 中求值时为真
  • yes-pattern is a regex to match given the condition is true yes-pattern是在条件为真时匹配的正则表达式
  • no-pattern is an optional regex to match given the condition is false如果条件为假, no-pattern是一个可选的正则表达式来匹配

Therefore, you can't shove new text into the match using a conditional.因此,您不能使用条件将新文本推送到匹配项中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM