NGINX - 正则表达式 - 搜索整个位置以查找非字母数字

时间:2015-08-27 19:46:17

标签: regex nginx

我的vhost conf中有以下正则表达式:

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

但它似乎只匹配第一个字符:

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

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

我尝试过的变化:

# 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]+ {

感谢。

1 个答案:

答案 0 :(得分:3)

您可以尝试以下规则:

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