I have setup an Apache HTTP server with VirtualHosts before a proprietary web server within the back. The after sales server are only able to have one (1) level in the ID pathways therefore the following public Web addresses:

http://public-server/path1/path2/path3?querystring-parameters

ought to be converted for that after sales to:

http://internal-server/path1/path2/page/<path1>_<path2>_<path3>?querystring-parameters

Observe that there might be a variety of path1, path2, path3, path4, .... plus they really should (regardless of if perhaps 1 is available or multiple) be concatenated by having an underscore. Also observe that the querystring-parameters CAN contain '?', '/' and '_' therefore the rule shouldn't affect the querystring by any means.

I have attempted trying to find methods to this but can't learn how to overcome the issue. Any suggestions?

If you're able to show up some most of possible pathways, that you can do something for this effect:

# This will work for up to 5 paths
RewriteRule /([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)/?([^/]*) http://internal-server%{REQUEST_URI}$1_$2_$3_$4_$5 [L,QSA]

The /?([^/]*) can be included to the finish as numerous occasions since you need, together with added the related groups (_$6 ..) towards the rewritten URL.

Regrettably, there's absolutely no way possess a completely unknown quantity of pathways, yet still time rely on them within the rewritten URL. Also, the [QSA] flag will attach your querystring onto the submitted URL, untouched.

Hope this can help.