0

I build a new permalink structure on my website. After adding some nginx rewrite rules, there is a redirect loop to /vlogger/info/info/info/info/...

Old permalink:

/vlogger/user-name

New permalink:

/vlogger/info/user-name

current nginx rule:

location ~ /vlogger/(.*)$ {
    rewrite ^/vlogger/info/$1 permanent;
}

How can I check if the second directory ( /vlogger/"info/" ) alway set and redirect successful without a loop?

(*Username contains only: a-z0-9 and - (minus))

turbopixel
  • 11
  • 3

2 Answers2

1

Nginx terminates the search of regular expressions on the first match. Thus, just put a more specific match above it. You need to use a regular expression match again (imo) because prefix matches are only used when no regular expression matches. (I highly recommend to read the manual: http://nginx.org/en/docs/http/ngx_http_core_module.html#location)

location ~ /vlogger/info/(.*)$ {
    # whatever you want to do with them
}

location ~ /vlogger/(.*)$ {
    rewrite ^ /vlogger/info/$1 permanent;
}

Please note that regex is probably not necessary in your case (if you have other regex you might need to use regex tho, imo. Again, read the manual entry). You might get better performance with prefix strings...

location /vlogger/info/ {
    # whatever you want to do with them
}

location /vlogger/ {
    rewrite ^ /vlogger/info/$1 permanent;
}
Tero Kilkanen
  • 34,499
  • 3
  • 38
  • 58
user3648611
  • 141
  • 3
1

I found the solution:

rewrite ^/vlogger/([^/]*)$ /vlogger/info/$1 permanent;

This redirect from /vlogger/name to /vlogger/info/name

turbopixel
  • 11
  • 3