Apache RedirectMatch 指令的正则表达式

发布于 2025-01-11 06:08:48 字数 1545 浏览 0 评论 0原文

如果 URL 不以仪表板结尾,我尝试将所有用户请求重定向到不同的 URL。下面是 RedirectMatch 指令,我使用不同的 URL 模式在线正则表达式验证器站点测试了正则表达式,所有站点都显示正则表达式是正确的。

但由于某种原因,重定向没有发生。

RedirectMatch 301 ^/^((?!dashboard).)*$ https://abc.xyz.com/$1

请让我知道我是否在正则表达式中遗漏了一些内容。

阿帕奇配置文件

<VirtualHost *:443>
    ServerName staging.abc.com

    ## Logging
    LogLevel info
    CustomLog "/var/log/apache2/abc_https_access.log" combined
    ErrorLog "/var/log/apache2/abc_https_error.log"
    ServerSignature Off

    SSLEngine on
    SSLProtocol -All +TLSv1.2
    SSLCertificateFile /root/san/staging.abc.cer
    SSLCertificateKeyFile /root/san/staging.abc.key
    SSLCACertificateFile /root/san/abc.chain.cer
    SSLVerifyClient require

    RedirectMatch 301 ^/(?!.*dashboard$)(.*)$ https://abc.xyz.com/$1    

    ## Proxy rules
    ProxyRequests Off
    ProxyPreserveHost On

    # Preserve server status root
    ProxyPass /server-status !

    ProxyPass /rest http://localhost:8101/rest
    ProxyPassReverse /rest http://localhost:8101/rest

    
    <LocationMatch "^/rest">
        SSLOptions +StdEnvVars
        SSLRequire %{SSL_CLIENT_S_DN_CN} !~ m#(^testuser$)#i
        SSLVerifyClient require
        SSLVerifyDepth 2
        RequestHeader unset Authorization
        RequestHeader set user_id %{SSL_CLIENT_S_DN_CN}s
    </LocationMatch>

    <Directory />
        Order Deny,Allow
        Deny from all
        Options None
        AllowOverride None
    </Directory>

</VirtualHost>

I am trying to redirect all the user requests to a different URL if the URL does not end with the dashboard. Below is the RedirectMatch directive and I tested the regex with different URL patterns online regex validator sites and all sites show regex is correct.

But for some reason, redirection is not happening.

RedirectMatch 301 ^/^((?!dashboard).)*$ https://abc.xyz.com/$1

Please let me know whether I am missing some in the Regex.

Apache config file

<VirtualHost *:443>
    ServerName staging.abc.com

    ## Logging
    LogLevel info
    CustomLog "/var/log/apache2/abc_https_access.log" combined
    ErrorLog "/var/log/apache2/abc_https_error.log"
    ServerSignature Off

    SSLEngine on
    SSLProtocol -All +TLSv1.2
    SSLCertificateFile /root/san/staging.abc.cer
    SSLCertificateKeyFile /root/san/staging.abc.key
    SSLCACertificateFile /root/san/abc.chain.cer
    SSLVerifyClient require

    RedirectMatch 301 ^/(?!.*dashboard$)(.*)$ https://abc.xyz.com/$1    

    ## Proxy rules
    ProxyRequests Off
    ProxyPreserveHost On

    # Preserve server status root
    ProxyPass /server-status !

    ProxyPass /rest http://localhost:8101/rest
    ProxyPassReverse /rest http://localhost:8101/rest

    
    <LocationMatch "^/rest">
        SSLOptions +StdEnvVars
        SSLRequire %{SSL_CLIENT_S_DN_CN} !~ m#(^testuser$)#i
        SSLVerifyClient require
        SSLVerifyDepth 2
        RequestHeader unset Authorization
        RequestHeader set user_id %{SSL_CLIENT_S_DN_CN}s
    </LocationMatch>

    <Directory />
        Order Deny,Allow
        Deny from all
        Options None
        AllowOverride None
    </Directory>

</VirtualHost>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耳钉梦 2025-01-18 06:08:48

我正在尝试将所有用户请求重定向到不同的 URL 如果 URL 不以仪表板结尾

您可以使用此重定向规则:

RedirectMatch 301 ^/(?!.*dashboard$)(.*)$ https://abc.xyz.com/$1

(?!.*dashboard$) 是一个否定的前瞻,如果 URL 以 dashboard 结尾,则匹配失败。

I am trying to redirect all the user requests to a different URL if the URL does not end with the dashboard

You can use this redirect rule:

RedirectMatch 301 ^/(?!.*dashboard$)(.*)$ https://abc.xyz.com/$1

(?!.*dashboard$) is a negative lookahead to fail the match if URL ends with dashboard.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文