skip to Main Content

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:

https://example.com/conversion/index.php?countryfrom=US-United+States&countryto=IN-India&amount=123&amount_type=USD&recv_amount=INR

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


  1. 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

    rewrite  ^/conversion/(w{3})/(w{3})/(d*)$   /conversion/index.php?amount=$3&from_cur=$1&tocur=$2 last;
    

    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/

    Login or Signup to reply.
  2. server
    {
        listen 80;
        server_name example.com www.example.com;
    
        location ~ /conversion/(?<countryfrom>[^/]*)/(?<countryto>[^/]*)/(?<amount_type>[^/]*)/(?<recv_amount>[^/]*)/(?<amount>[^/]*)/?
        {
            alias /var/www/html/conversion/;
            index index.php;
    
            location ~* .php$
            {
                fastcgi_index index.php;
                fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME /var/www/html/conversion/index.php;  # hard-coded
                # fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;  # dynamic - unsure
                # fastcgi_param SCRIPT_FILENAME $request_filename;  # dynamic - unsure
                # fastcgi_param SCRIPT_NAME $fastcgi_script_name;  # dynamic - unsure
    
                fastcgi_param countryfrom $countryfrom;
                fastcgi_param countryto $countryto;
                fastcgi_param amount $amount;
                fastcgi_param amount_type $amount_type;
                fastcgi_param recv_amount $recv_amount;
            }
        }
    }
    
    <?php
    echo $_SERVER['countryfrom'];
    # …
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search