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

前端 未结 9 727
慢半拍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 / {
        ....
        }
    }
    
    0 讨论(0)
  • 2020-11-28 03:42

    If nginx behind proxy with https, this snippet do correct redirect for $scheme

    map $http_x_forwarded_proto $upstream_scheme {
        "https" "https";
        default "http";
    }
    
    server {
        ...
        location / {
            rewrite ^([^.\?]*[^/])$ $upstream_scheme://$http_host$1/ permanent;
        }
        ...
    }
    

    And on the upstream proxy pass the X-Forwarded-Proto header like:

    location / {
        proxy_set_header X-Forwarded-Proto $scheme;
        ...
    }
    
    0 讨论(0)
  • 2020-11-28 03:48

    More likely I think you would want something like this:

    rewrite ^([^.]*[^/])$ $1/ permanent;
    

    The Regular Expression translates to: "rewrite all URIs without any '.' in them that don't end with a '/' to the URI + '/'" Or simply: "If the URI doesn't have a period and does not end with a slash, add a slash to the end"

    The reason for only rewriting URI's without dots in them makes it so any file with a file extension doesn't get rewritten. For example your images, css, javascript, etc and prevent possible redirect loops if using some php framework that does its own rewrites also

    Another common rewrite to accompany this would be:

    rewrite ^([^.]*)$ /index.php;
    

    This very simply rewrites all URI's that don't have periods in them to your index.php (or whatever file you would execute your controller from).

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

    using the rewrites from anthonysomerset in a Wordpress, I experimented problems accesing to /wp-admin dashboard due to reirection loop. But i solve this problem using the above conditional:

    if ($request_uri !~ "^/wp-admin")
    {
    rewrite ^([^.]*[^/])$ $1/ permanent;
    rewrite ^([^.]*)$ /index.php;
    }
    
    0 讨论(0)
  • 2020-11-28 03:50
    rewrite ^([^.\?]*[^/])$ $1/ permanent;
    

    to avoid querystrings of a rest url getting a / tagged on.

    e.g.

    /myrest/do?d=12345

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

    Odd that this is the first result in Google, but doesn't have a satisfactory answer. There are two good ways to do this I know of. The first is to straight-up check if the request will hit a file and only apply a rewrite condition if not. E.g.

    server {
       # ...
       if (!-f $request_filename) {
         rewrite [^/]$ $uri/ permanent;
       }
       location / {
          # CMS logic, e.g. try_files $uri $uri /index.php$request_uri;
       }
       # ...
    }
    

    The second, which many prefer as they'd rather avoid any use of if that isn't 100% necessary, is to use try_files to send the request to a named location block when it won't hit a file. E.g.

    server {
       # ...
       location / {
          try_files $uri $uri/ @cms;
       }
       location @cms {
          rewrite [^/]$ $uri/ permanent;
          # CMS logic, e.g. rewrite ^ /index.php$request_uri;
       }
       # ...
    }
    
    0 讨论(0)
提交回复
热议问题