You could use 2 of WordPress’ built in functions to get the result you’re after.
add_rewrite_tag() and add_rewrite_rule()
There is an example of how to use these tags to get the result you want about halfway down this page https://codex.wordpress.org/Rewrite_API/add_rewrite_rule.
Note: remember to flush your rewrite rules.