I currently have the working Mod Rewrite Regex:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*/)?((?:cmd)[^/]*)/((?!(?:cmd)[.+]*)(.+)) $1?$2=$3&%1 [L]
That regex takes the following URL and transforms it into the URL immediately below:
www.site.com/cmd1/param/cmd2/param2/stillparam2 and turn it into www.site.com/index.php?cmd1=param&cmd2=param2/stillparam2
That works fine, but I would also like to create another negative lookahead assertion to ensure that a URL block – ie a /texthere/ param – doesn’t include an underscore. An invalid string might look like: www.test.com/cmd/thing/getparam_valuehere; the regex should parse the cmd/thing as a key and value pair and ignore the rest of the string. I would then also write another RewriteRule to have the block of the URL with the underscore in it added as another URL parameter. The following URL translation would occur:
www.test.com/cmd/param1/cmd2/directory/param2/sortorder_5
www.test.com?cmd=param1&cmd2=directory/param2&sortorder=5
Please let me know if I have not been clear enough. Any help would be great.
NB: I have tried using a negative lookahead nested inside the one already present – (?!(?!)) – and have tried using an | on two negative lookaheads, but neither solutions worked. I thought that perhaps something else was more fundamentally wrong?
Thanks all.
Edit: I have also tried the following – which I really thought would work (but obviously, didn’t!)
RewriteRule ^(.*/)?((?:cmd)[^/]*)/((?!(?:cmd)[.+]*)(?![.+]*(?:_)[.+]*)(.+)) $1?$2=$3&%1 [L]
That does the following:
www.test.com/cmd/param1/sortorder_1/ translates to
www.test.com?cmd=param1/sortorder_1/
When it should instead become: www.test.com?cmd=param1&sortorder=2/. The rule to translate /sortorder_2/ into&sortorder=2 has not yet been created, but you can hopefully see what I mean).
After about four days of experimenting, I ended up with a somewhat different solution than I had originally expected to find. I simply removed all the actual URL manipulation to my index.php file and routed all requests through there. Here is my (much cleaner) .htaccess file:
and here is the block of code I used to parse the entered URL:
preg_match_all(‘|/([A-Za-z0-9]+)((?!/)[A-Za-z0-9-.]*)|’, $_GET[‘path’], $matches);
On a URL like this:
It successfully produces these separate arrays which I then use to handle requests:
Thanks to all who took the time to read and reply.