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

前端 未结 9 730
慢半拍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.

    0 讨论(0)
  • 2020-11-28 03:54

    For nginx:

    rewrite ^(.*[^/])$ $1/ permanent;
    
    0 讨论(0)
  • 2020-11-28 03:56

    Try this: ^(.*)$ http://domain.com/$1/ [L,R=301]

    This redirects (Status code 301) everything ($1) without a "/" to "$1/"

    0 讨论(0)
提交回复
热议问题