Search code examples
urlnginxnginx-locationcanonical-link

How to configure redirects to url without trailing slash in nginx?


I want to redirect URLs with slash to the path without trailing slash. So /some-url/ to /some-url for Duplicate Content Issue

And the rest of the URLs, like

/some-url.xml
/some-url?
/some-url/?
/some-url?q=v
/some-url/?q=v
/some-url

Should stay without redirection. i want exactly opposite to following question. How to configure redirects to url with trailing slash in nginx?


Solution

  • After verifying all the nginx configuration i dint find any else condition and any AND (&&) conditions. finally i did with "set" property in nginx

      set $val 0;
      if ($request_method ~ "^GET$"){
          set $val 1;
       }
    
      if ($request_uri ~ ^/(.*)/$ ) {
       set $val "${val}1";
      }
    
      location / {
              if ($val ~ 11) { rewrite ^/(.*)/$ https://$host/$1; }
              try_files $uri $uri/ /index.php$is_args$args;
      }