How to register RewriteRule?

Hello! I need to write a rule to RewriteEngine according to the following scheme:

  • 2 optional named parameters: method and id
  • They may have both or one or none
  • After them after the question mark can go indefinitely any parameter defined as STRING QIERY
  • trailing slash can both be and not be

Here are examples of what behavior is expected.
# If after api / is one word that is the name of the method #
http://site.ru/api/items http://site.ru/api/index.php?method=items
http://site.ru/api/items/ http://site.ru/api/index.php?method=items

# If after api / is 2 words, the first is the method name, and the second is the ID #
http://site.ru/api/items/42 http://site.ru/api/index.php?method=items&id=42
http://site.ru/api/items/42/ http://site.ru/api/index.php?method=items&id=42

# If after api / is one word, followed by a "?" character and a QUERY_STRING, #
# this method name and some parameters from the QUERY_STRING #
http://site.ru/api/items?mode=silent http://site.ru/api/index.php?method=items&mode=silent
http://site.ru/api/items/?mode=silent http://site.ru/api/index.php?method=items&mode=silent

# If after api / is 2 words followed by the sign "?" and a QUERY_STRING, #
# the first is the method name, and the second is the ID #
# and some parameters from the QUERY_STRING #
http://site.ru/api/items/42?mode=silent http://site.ru/api/index.php?method=items&id=42&mode=silent
http://site.ru/api/items/42/?mode=silent http://site.ru/api/index.php?method=items&id=42&mode=silent

# Parameters besides the method id can be anything, and their number is also #
http://site.ru/api/items?mode=silent&foo=bar&fizz=buzz http://site.ru/api/index.php?method=items&mode=silent&foo=bar&fizz=buzz

# Of course, instead of the items can be any other word #
# As is 42 – so they cannot be tied #


I wrote the following conditions:
RewriteEngine On

RewriteRule ^api/([^/]*)/([^/]*)/$ /api/index.php?method=$1&id=$2 [L]
RewriteRule ^api/([^/]*)/$ /api/index.php?method=$1 [L]


And it works, but with this decision I cannot Express arbitrary URL in the GET parameters through QUERY_STRING.
Please tell me how to append or rewrite conditions to work as needed?
July 9th 19 at 10:59
1 answer
July 9th 19 at 11:01
Solution
Try:
or [L] -> [L,QSA]
or /api/index.php?method=$1 -> /api/index.php?method=$1&%{QUERY_STRING}
Thanks, adding QSA helped! - Alysson_Gaylord commented on July 9th 19 at 11:04

Find more questions by tags ApachehtaccessProcessing URL