Evgeniy
Evgeniy

Reputation: 2605

How to get request_uri without arguments on Nginx

There is a similar question already, but i need an advice how to put both needed parts together:

I need a following construction:

location ~*     {
if ($args ~ *)      {
add_header Link "<$scheme://$http_host$request_uri>; rel=\"canonical\"";
}
}

where $request_uri must be non-encoded url (can't use $uri). How is this way to get rid of args:

map $request_uri $request_uri_path {
  "~^(?P<path>[^?]*)(\?.*)?$"  $path;
}

to get into my example?

Upvotes: 1

Views: 3202

Answers (2)

Alexei Martchenko
Alexei Martchenko

Reputation: 331

I didn't find any good regex so I came up with this one:

^(?P<path>.*?)(\?.*)*$

You can use in your maps

map $request_uri $request_uri_no_parameters {
  "~^(?P<path>.*?)(\?.*)*$"  $path;
}

Here are my tests - https://regex101.com/r/K49bFm/2

Upvotes: 3

Evgeniy
Evgeniy

Reputation: 2605

For those who'll need this later: the whole context looks like:

http {
        map $request_uri $request_uri_path {
        "~^(?P<path>[^?]*)(\?.*)?$" $path;
        }

        server {

                location ~* {
                if ($args ~ .) {
                add_header Link "<$scheme://$http_host$request_uri_path>; rel="canonical"";
                               }
                            }

              } 

     }

Upvotes: 0

Related Questions