Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

213
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!