Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

220
Visualizações
Rewrite query string with N parameters and replace spaces

I'd need to remove the query string from the url

site.com/somepage?p1=something1&p2=something2&..pn=somethingn

and turn it into something like:

site.com/somepage/something1/something2/../somethingn

I've tried this but it doesn't suit what I'm trying to achieve..

RewriteCond %{QUERY_STRING} ^(.*)lang=([a-z]{2})&?(.*)$
RewriteRule (.*) /%2/$1?%1%3 [R=307]

also when passing a value with spaces in the string, I need to rewrite it to '-' but I can't figure it out how to do it

I use apache 2.4 on ubuntu 16.04

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Since the number of parameters seems "infinite" in your case, you can create a loop to rewrite them:

RewriteCond %{QUERY_STRING} ^[^&=]+=([^&]+)&?(.*)$
RewriteRule ^(somepage)(/.+[^/])?$ /$1$2/%1?%2 [R=301,L]

This will work as follows:

1) http://domain.tld/somepage?p1=something1&p2=something2&pn=somethingn
2) http://domain.tld/somepage/something1?p2=something2&pn=somethingn
3) http://domain.tld/somepage/something1/something2?pn=somethingn
4) http://domain.tld/somepage/something1/something2/somethingn

Update

I didn't notice your second requirement which was when passing a value with spaces in the string, I need to rewrite it to '-'. For that, you can combine it with another rule, such as:

RewriteCond %{QUERY_STRING} ^(.+?)(?:\s|%20)(.+)$
RewriteRule ^(.*)$ /$1?%1-%2 [R=301,L]

RewriteCond %{QUERY_STRING} ^[^&=]+=([^&]+)&?(.*)$
RewriteRule ^(somepage)(/.+[^/])?$ /$1$2/%1?%2 [R=301,L]

The higher N is (and the more spaces you have in the query string), the higher the number of redirects.

Let's say the initial url is http://domain.tld/somepage?p1=some thing1&p2=some%20thing2&pn=somethingn (spaces encoded or not). In this case, it will work as follows:

http://domain.tld/somepage?p1=some thing1&p2=some%20thing2&pn=somethingn
http://domain.tld/somepage?p1=some-thing1&p2=some%20thing2&pn=somethingn
http://domain.tld/somepage?p1=some-thing1&p2=some-thing2&pn=somethingn
http://domain.tld/somepage/some-thing1?p2=some-thing2&pn=somethingn
http://domain.tld/somepage/some-thing1/some-thing2?pn=somethingn
http://domain.tld/somepage/some-thing1/some-thing2/somethingn
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda