0

I'm trying to rewrite URLs such as

/product/16/var1/value1/var2/value2...

to this

index.php?page=product&id=16&var1=value1&var2=value2...

In other words, I would like to have a "main parameter" translated to an id (and I can do this), but I would also like to have, from that point on, couples of "directories" translated recursively to key-value pairs.

Is this possible with Apache mod_rewrite?

Simone
  • 962
  • 1
  • 12
  • 23
  • Possible duplicate of [Reference: mod\_rewrite, URL rewriting and "pretty links" explained](http://stackoverflow.com/questions/20563772/reference-mod-rewrite-url-rewriting-and-pretty-links-explained) –  Jan 05 '16 at 17:18
  • Give specific examples of what you want and if you already have rules in your .htaccess. – Panama Jack Jan 05 '16 at 17:19
  • @PanamaJack, I have nothing yet. I'm trying to understand if this can be done. – Simone Jan 05 '16 at 17:32
  • Anything can be done, there's no point in asking if it can be. I suggested that you give specific examples of the URL's format you want to use and how they should be instead of a general example you gave. – Panama Jack Jan 05 '16 at 17:34
  • @RC., I've edited the question to be clearer. Stress on the word "recursively" – Simone Jan 05 '16 at 17:34
  • @PanamaJack, let's say I have `/category/21/orderby/date-desc/from/20/to/30`... but other pages may well have other parameters that I still haven't thought of! The point is, I am fully capable of matching certain patterns (for example, only numeric chars), but I have a hard time figuring out the recursive part of it. – Simone Jan 05 '16 at 17:37

1 Answers1

1

In the absence of the [L] flag, any mod_rewrite rule will apply repeatedly to any URI which corresponds to the rule's rewrite conditions and pattern.

Knowing this, we can build a mod_rewrite rule which looks for any URIs with query strings beginning in a certain way and then repeatedly harvests the folder-names of that URI (two at a time) to build the rest of the query string.

See example below:

In the root folder of

http://example.com/

save an .htaccess file with the following mod_rewrite directives:

RewriteEngine On

RewriteRule ^(product)/([0-9]{2})/(.*) http://%{HTTP_HOST}/$3/index.php?page=$1&id=$2

RewriteCond %{QUERY_STRING} ^(page=product&id=[0-9]{2}.*)
RewriteRule ^([^/]+)/([^/]+)/(.*/)?index.php$ http://%{HTTP_HOST}/$3index.php?%1&$1=$2

Using the above:

http://example.com/product/16/var1/value1/var2/value2/

becomes

http://example.com/index.php?page=product&id=16&var1=value1&var2=value2

and

http://example.com/product/16/var1/value1/var2/value2/var3/value3/var4/value4/

becomes

http://example.com/index.php?page=product&id=16&var1=value1&var2=value2&var3=value3&var4=value4
Rounin
  • 21,349
  • 4
  • 53
  • 69
  • 1
    Thanks @Rounin, this seems to be exactly what I'm looking for. However, I've tested it on my machine (Windows-based Apache) and I'm not getting the expected result: instead, only the last two folders are transformed to query string parameters (so I get `?page=var4&id=value4`). When I tested it with an online RewriteRule tester, however, it is correct. May my issue be due to some peculiarity of the Windows version? – Simone Jan 06 '16 at 00:09
  • I've tested it on a live Linux Apache server - and it's definitely working. I don't have a Windows server to hand to test on. Not sure what to suggest. – Rounin Jan 06 '16 at 00:38
  • 1
    I realized what happened: I had dropped your rules in an .htaccess of mine, that already had other rules in it. I edited your rules to fit: for example, adding [L] prevents following (more general) rules from matching, thus adding twice the path info postfix. Thanks for the help: without your starting point, I wouldn't have been able to do it. – Simone Jan 07 '16 at 14:08