1

This is the URL that I need to remove the last segment (?order=asc)

https://www.example.com/segment_1/segment_2/page/the_number_goes_here/?order?asc

This is the code I'm trying:

location ~ ^/([^/]+)/([^/]+)/page/(\d+)/ {
    try_files $uri $uri/ /index.php?$args;
    # Capture the 2 segments and page number
    set $segment_1 $1;
    set $segment_2 $2;
    set $page_number $4;
    # Redirect without the sufix
    rewrite ^ /$segment_1/$segment_2/page/$page_number/ permanent;
}

1 Answer 1

0

The location and rewrite directives process the normalised URI which does not include the query string (which you have called "the last segment").

The query string is available in the $args variable.

The rewrite directive can optionally remove the original query string by appending a ? to the end of the replacement value.

For example:

rewrite ^ /$segment_1/$segment_2/page/$page_number/? permanent;

Also, in your example code:

  • The page number is captured in $3 and not $4
  • The try_files statement is probably redundant as Nginx will always evaluate the set and rewrite directives first.

is evaluated first

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .