I
I
Ivan2021-10-01 19:46:39
Nginx
Ivan, 2021-10-01 19:46:39

How to add slash at the end of url in Laravel site using nginx?

There is a site on Laravel. The site has a blog /blog. The site has categories, /blog/namecategory. If you choose to filter all categories, it will be /blog/all.

I need to configure NGINX to always remove the slash for /blog/all and always add a slash to the end of /blog/namecategory to be /blog/namecategory/

How can I do this?

I tried to do like this:

location /blog {
        rewrite ^(.*[^/])$ $1/ permanent;
    }


Then the slash is added, but 404 comes out from nginx.
61573b6e1ca9c403496400.png

UPD:
Also a good way that works:
if ($request_uri !~ "(\.html|\/)$") { return 301 "$request_uri/"; }

But it works for the whole site. If I wrap it in location /blog {} it WILL add a slash only in the blog but give a 404.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dodo512, 2021-10-01
@youmixx

location /blog/ {
    rewrite ^/blog/all$  /index.php  last;
    
    rewrite ^(/blog/all)/$  $1  permanent;
    
    rewrite ^(/blog/.*[^/])$  $1/  permanent;
    
    rewrite ^ /index.php last;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question