简体   繁体   中英

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. 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>

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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