rewrite url with parameters in nginx

How To Rewrite URL With Parameters in NGINX

Many times, we need to redirect URL with parameters in NGINX to a new location. Here’s how to rewrite URL with parameters in NGINX. In other words, we will learn how to redirect query string or part of URL in NGINX.

 

How To Rewrite URL With Parameters in NGINX

Here are the steps to rewrite URL with parameters in NGINX. After you redirect URL in NGINX, you may want to use a reporting software to monitor the key metrics about your website/application such as signups, traffic, sales, revenue, etc. using dashboards & charts, to ensure everything is working well.

 

1. Open NGINX configuration file

If you are using NGINX’s main configuration file nginx.conf, without virtual hosts, then run the following command

$ sudo vi /etc/nginx/nginx.conf

 

If you have configured separate virtual hosts for your website (e.g www.mysite.com), such as /etc/nginx/sites-enabled/mysite.conf then open it with the following command

$ sudo vi /etc/nginx/sites-enabled/mysite.conf

 

2. Rewrite URL with Parameters in NGINX

There are multiple ways to rewrite URL with parameters in NGINX. We will look at each of them individually.

Let’s say you want to rewrite a single specific type of URL with parameters (e.g www.mysite.com/product-list?category=value1&Id=value2) to another URL (e.g www.mysite.com/product-list/value1/value2) without affecting the remaining URLs

In this case, add the following line in location / block to specifically rewrite along with parameters.

location / {
    if ($args ~* "/product-list?param1=val1&param2=val2") {
        rewrite ^ http://www.mysite.com/product-list/$arg_param1/$arg_param2? last;
    }
}

In the above code, we use IF statement to redirect only those URLs whose patterns match our requirement. We use built-in server variables $arg_param1, $arg_param2, which store parameter values, to define our new URL pattern.

 

Similarly, if you want to redirect all URLs of your website directory (e.g /product-list) with parameters to another directory (e.g product-info),

location / {
    if ($args ~* "/product-list?param1=val1&param2=val2") {
        rewrite ^ http://www.mysite.com/product-info/$arg_param1/$arg_param2? last;
    }
}

 

However, if you want to rewrite all URLs of your website (e.g www.mysite.com) with parameters to another domain (e.g www.newsite.com), it is easier to use return instead of rewrite

server {
    listen 80;
    listen 443 ssl;
    server_name mysite.com;
    return 301 $scheme://www.newsite.com$request_uri;
}

$request_uri is a server variable in NGINX that stores URI part of URL along with all parameters.

Bonus Read: How to Move NGINX Web Root to New Location

 

3. Restart NGINX Server

Run the following command to check syntax of your updated config file.

$ sudo nginx -t

 

If there are no errors, run the following command to restart NGINX server.

$ sudo service nginx reload #debian/ubuntu
$ systemctl restart nginx #redhat/centos

 

That’s it! Now your NGINX server will automatically redirect URLs with parameters to their new location.

By the way, if you want to create charts & dashboards to monitor your business or website, you can try Ubiq. We offer a 14-day free trial.

mm

About Ubiq

Ubiq is a powerful dashboard & reporting platform for small & medium businesses. Build dashboards, charts & reports for your business in minutes. Get insights from data quickly. Try it for free today!