I have an index.php
file inside a directory on my server running nginx. It does some currency conversion calculations.
I need to be able to send a GET request to this file that includes 5 URL parameters.
Example:
But … this is an ugly URL for humans, so I want to make it prettier, and therefore convert it to:
https://example.com/conversion/US-United-States/IN-India/USD/INR/123
I think I figured out this part (but it’s possibly not the most elegant solution):
rewrite ^/conversion/index.php /conversion/$arg_countryfrom/$arg_countryto/$arg_amount_type/$arg_recv_amount/$arg_amount? last;
However … index.php
needs to be able to read those variables, and it can’t if they’re stored as URL paths (at least I don’t think so?), so somehow in the backend nginx needs to convert it back to ?countryfrom=US...
format.
Any suggestions? Thanks in advance!
2
Answers
I do not understand why do you need the countries for a currency concerter but if you really need this use the ISO codes of the countries
https://example.com/conversion/US/IN/USD/INR/123
if not use the currencies only
https://example.com/conversion/USD/INR/123
and then rewrite this to
https://example.com/conversion/index.php?countryfrom=US&countryto=IN-&amount=123&amount_type=USD&recv_amount=INR
or https://example.com/conversion/index.php?amount=123&from_cur=USD&to_cur=INR
Your rewrite rule is wrong.
You need an regex to analyze the pattern and rewrite it into the new URL
https://example.com/conversion/USD/INR/123
you can test your regex here: https://regex101.com/ (CAUTION: here you will have to escape the slashes with backslashes – remove them again to use for apache/nginx)
more infos on Nginx rewriting
https://www.nginx.com/blog/creating-nginx-rewrite-rules/
The advantage of named variables is that you do not mix them with variables from another scope, such as the http, server or location block.
Untested.