简体   繁体   English

Apache RedirectMatch 指令的正则表达式

[英]Regex for Apache RedirectMatch directive

I am trying to redirect all the user requests to a different URL if the URL does not end with the dashboard.如果 URL 不以仪表板结尾,我试图将所有用户请求重定向到不同的 URL。 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.下面是 RedirectMatch 指令,我用不同的 URL 模式在线正则表达式验证器站点测试了正则表达式,所有站点都显示正则表达式是正确的。

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 Apache 配置文件

<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如果 URL 不以仪表板结尾,我正在尝试将所有用户请求重定向到不同的 URL

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 .如果 URL 以dashboard结尾,则(?..*dashboard$)是使匹配失败的否定前瞻。

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

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