regex - Apache LocationMatch 正则表达式

标签 regex apache proxy mod-proxy locationmatch

我的问题

我需要有Apache HTTP Server (v2.4.10) 代理对Tomcat 的动态应用请求,这不仅与Tomcat 中的路径不匹配,而且彼此的路径也相似。例如:
/products/<category>/<sub-category>/<sub-sub-category>/<product-id>.html 代理到:http://mycluster/pf/<product-id>.html
...并且...
/products/<category>/<sub-category>/<sub-sub-category>/<anything-not-ending-in-html> 代理到:http://mycluster/search/<anything-not-ending-in-html>
我的尝试

我正在尝试使用 LocationMatch 正则表达式来处理这个问题,但并没有完全成功。以下 LocationMatch 正则表达式独立工作(将 *.html 请求代理到 <tomcat>/pf/*.html ):

<LocationMatch ^/products/(?<cat>.+)/(?<subcat>.+)/(?<subsubcat>.+)/(?<partnum>.+).html>
ProxyPass balancer://mycluster/pf/%{env:MATCH_PARTNUM}.html
ProxyPassReverse balancer://mycluster/pf/%{env:MATCH_PARTNUM}.html
</LocationMatch>

这使用以下示例路径传递 URL:/products/aaa/bbb/ccc/ddd3456.html(这是正确的)

但是,当我还启用下面的正则表达式时:
<LocationMatch ^(?!.*\.html$)/products/(?<cat>.+)/(?<subcat>.+)/(?<subsubcat>.+)((/?)|(./*))$>
ProxyPass balancer://mycluster/search/
ProxyPassReverse balancer://mycluster/search/
</LocationMatch>

尝试访问 /products/aaa/bbb/ccc/ 会导致 404 页面。
在这里,我期待对“/products/aaa/bbb/ccc/”的任何不以 .html 结尾的请求传递给/search/(包括要包含的任何后续路径信息:例如 .../search/相比 )

我的问题

我无法完全弄清楚出了什么问题。根据 Rubular 提供的正则表达式是正确的:
  • Search Regex
  • Product Regex

  • 我在这里缺少什么?

    我很感激任何关于解决这个问题的建议!

    最佳答案

    正则表达式似乎有点过于宽松了 - .+ 的范围在 cat/subcat/subsubcat 中需要稍微限制。最后的表达式 ("./*") 也有轻微的错误,这应该是 ("/.*") :

    工作地点匹配:

    <LocationMatch ^(?!.*\.html$)/products/(?<cat>([A-Za-z0-9\-\_])+)/(?<subcat>([A-Za-z0-9\-\_])+)/(?<subsubcat>([A-Za-z0-9\-\_])+)((/?)|(/.*))$>
    ProxyPass balancer://mycluster/search/
    ProxyPassReverse balancer://mycluster/search/
    </LocationMatch>
    

    关于regex - Apache LocationMatch 正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25052661/

    相关文章:

    Apache 没有将 .co.uk 重定向到 .com

    java - eclipse 上的http 404错误

    PHP 将警告设置为致命

    powershell - Powershell Invoke-WebRequest返回 “remote name could not be resolved”,但不返回代理错误

    proxy - gCloud SDK 无法在企业代理后面的 macOS 中安装

    Python:子类别的正则表达式查找?

    regex - Perl-regex 在应该区分大小写时不区分大小写

    python - 我怎样才能删除 "(",")"?

    eclipse - Eclipse 的 Maven 插件,代理问题

    r - 提取文本列中特定单词后的第一个数字