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

前端 未结 9 843
慢半拍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:41

    it's too late but I want to share my solution, I've met issue with trailing slash and nginx.

    #case : 
    # 1. abc.com/xyz  => abc.com/xyz/
    # 2. abc.com/xyz/ => abc.com/xyz/
    # 3. abc.com/xyz?123&how=towork => abc.com/xyz/?123&how=towork
    # 4. abc.com/xyz/?123&ho=towork => abc.com/xyz/?123&how=towork
    

    and this is my solution

    server { 
        ....
        # check if request isn't static file
        if ($request_filename !~* .(gif|html|jpe?g|png|json|ico|js|css|flv|swf|pdf|xml)$ ) {
           rewrite (^[^?]+[^/?])([^/]*)$ $1/$2 permanent;
        }
        ....
        location / {
        ....
        }
    }
    

提交回复
热议问题