Rewrite with regex

Artur nginx at
Mon Apr 12 09:30:30 UTC 2021

Hello !

I have the following setup :

location / {
 try_files $uri $uri/ @shortnames;
location @shortnames {
 rewrite "^/([a-zA-Z0-9]{1,32})$" /index.php?short_name=$1 last;
 return 404;

I filter the 'shortnames' URI to have a format similar to /dfg6df4g64
with minimum and maximum size, only digits and letters allowed. Working

I want to add hyphen in the 'shortnames' so /ad5-ff56 or
/5f9-dfdf4-55f-ddfg are allowed ( but not /-ff4fg or /dgfgdf- ).
The regex that matches these conditions is something like (without the
size match) : ^/([a-zA-Z0-9]+([-][a-zA-Z0-9])*)$
However there is no match and rewrite fails.

My current setup is a Debain 10 server with nginx-full (14.2.2) installed.

Any idea on how I can solve this problem, please ?


Best regards,

More information about the nginx mailing list