I want to rewrite pretty urls to a query parameter in my .htaccess file using regex.
So that:
https://www.example.com/s/file/name1/value1/name2/value2/name3/value3
gets rewritten to:
https://www.example.com?file.htm?name1=value1&name2=value2&name3=value3
It needs to handle variable numbers of name pair values. It will include at least one name value pair. Eg it needs to work with https://www.example.com/s/file/name1/value1 and https://www.example.com/s/file/name1/value1/name2/value2
The s in https://www.example.com/s/file/name1/value1/name2/value2/name3/value3 indicates the rewrite rule should trigger: all other urls should be left alone. The file value is the name of the htm file, so this can have different values.
In regex101.com I have tried:
- pattern:
\/([^\/]+)(\/)([^\/]+) - substitution:
$1=$3& - On string:
/s/new/v/123/c/42 - And it returns:
s=new&v=123&c=42&
But it should return: new.htm?v=123&c=42
So I have successfully gotten it to work with a variable number of name value pairs. But I just can't get my head around how to make it first move past s and new and then dynamically replace name value pairs.
I did not include https://www.example.com/ in regex101 because in a .htaccess file the initial domain is assumed.
I found this method but it seems to work with a fixed amount of value pairs.
I also reviewed this post which contains great information, but no solution to this specific issue.