Rewrite with regex

Francis Daly francis at
Mon Apr 12 12:46:47 UTC 2021

On Mon, Apr 12, 2021 at 02:21:15PM +0200, Artur wrote:
> Le 12/04/2021 à 12:19, Francis Daly a écrit :
> > On Mon, Apr 12, 2021 at 12:02:32PM +0200, Artur wrote:

Hi there,

> In fact it works as is. The only problem is that there is no simple way
> to check the total string length.
> But having several rewrite rules may be also a good idea.

You had

  rewrite "^/([a-zA-Z0-9]{1,32})$" /index.php?short_name=$1 last;

For brevity here, let's just use "lower case letter" instead of "letter
or number", so effectively you had

  rewrite "^/([a-z]{1,32})$" /index.php?short_name=$1 last;

and now you want to also allow "-" anywhere except the start or end.


  rewrite "^/([a-z][-a-z]{0,30}[a-z])$" ...

do what you want in most cases?

And does

  rewrite "^/([a-z])$" ...

do what you want in the other cases?

Francis Daly        francis at

More information about the nginx mailing list