Ad

NGINX Return 404 Error When Adding A Trailing Slash

- 1 answer

Today, I have a problem with NGINX.

In my NGINX config I have this :

location ~ / {
    try_files $uri $uri/ /index.php?$query_string;
}

For example, I want to access https://my.domain/hello with a route defined with Laravel. It works but now if I access https://my.domain/hello/ it returns a 404 NGINX error page. I also point out that I use Plesk.

Do you have any ideas on how to fix that ?

Thank you for your time ;)

Ad

Answer

Add 301 redirect if path is not directory and ending on /

if (!-d $request_filename) {
   rewrite ^/(.*)/$ /$1 permanent;
}

location / {
    try_files $uri $uri/ /index.php?$query_string;
}
Ad
source: stackoverflow.com
Ad