Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NGINX - Regex - Search entire location for non alphanumeric

Tags:

regex

nginx

I have the following regex in my vhost conf:

location ~* ^/!/[^a-zA-Z0-9] {
    return 301 $scheme://$http_host;
}

But it only appears to match the first character:

# Redirects to https://shouttag.com correctly
https://shouttag.com/!/!pink

# Does not redirect as expected
https://shouttag.com/!/p!nk

Variations I have tried:

# Assume that $ is unnecessary b/c I don't know what the end of the url may be
location ~* ^/!/[^a-zA-Z0-9]$ {

# Only seems to work when capturing data via group syntax ()
location ~* ^/!/[^a-zA-Z0-9]+ {

Thanks.

like image 297
Mike Purcell Avatar asked Nov 15 '25 13:11

Mike Purcell


1 Answers

You can try this rule:

location ~ ^/!/[a-zA-Z0-9]*[^a-zA-Z0-9].*$ {
    return 301 $scheme://$http_host;
}
like image 135
anubhava Avatar answered Nov 17 '25 08:11

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!