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
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
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