Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

149
Vistas
Replacing a character in $args

I want to replace each + with (space) in $args.

I know the solution of

if ($args ~ ^(.*)\+(.*)$) {
  return 301 "$uri?$1 $2";
}

However that's not ideal because there's a redirect for each +. Is there a better solution?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You could use more than one regular expression. Each regular expression tests the $args parameter for one, two, three, etc. + characters (evaluated in reverse order) which will minimise the recursion.

A compact solution uses a map to define the regular expressions.

For example:

map $args $spaces {
    default 0;
    ~^(.*)\+(.*)\+(.*)\+(.*)\+(.*)$  "$1 $2 $3 $4 $5";
    ~^(.*)\+(.*)\+(.*)\+(.*)$        "$1 $2 $3 $4";
    ~^(.*)\+(.*)\+(.*)$              "$1 $2 $3";
    ~^(.*)\+(.*)$                    "$1 $2";
}
server {
    ...
    if ($spaces) { return 301 "$uri?$spaces"; }
    ...
}

You can use more or less regular expressions to optimise the solution. The above will generate one redirection for up to four + characters, and two redirections for between five and eight + characters, and so on.

See this document for details.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda