Add slash to the end of every url (need rewrite rule for nginx)

前端 未结 9 883
慢半拍i
慢半拍i 2020-11-28 03:28

I try to get an \"/\" to every urls end:

example.com/art

should

example.com/art/

I use nginx as webserver.

I need the rewrite rule f

9条回答
  •  日久生厌
    2020-11-28 03:52

    server {
        # ... omissis ...
    
        # put this before your locations
        rewrite ^(/.*[^/])$ $1/ permanent;
    
        # ... omissis ...
    }
    

    If you want some kind of requests (say other than GET ones) to be prevented from doing this (usually it's about POST requests, as rewrite turns any request method into GET, which may break some of your site's dynamic functionality), add an if clause:

    server {
        # ... omissis ...
    
        # put this before your locations
        if ($request_method = "GET" ) {
            rewrite ^(/.*[^/])$ $1/ permanent;
        }
    
        # ... omissis ...
    }
    

    You can also put the rewrite in a location block (if too), to make it more specific.

提交回复
热议问题