Add slash to the end of every url (need rewrite ru

2020-01-24 19:26发布

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 for this..

For better understanding check this:

http://3much.schnickschnack.info/art/projekte

If u press on a small thumbnail under the big picture it reloads and shows this url:

http://3much.schnickschnack.info/art/projekte/#0

If i now have a slash on all urls (on the end) it would work without a reload of the site.

Right now i have this settings in nginx-http.conf:

server {
  listen *:80;
  server_name 3much.schnickschnack.info;
  access_log /data/plone/deamon/var/log/main-plone-access.log;
  rewrite ^/(.*)$ /VirtualHostBase/http/3much.schnickschnack.info:80/2much/VirtualHostRoot/$1 last;
  location / {
    proxy_pass http://cache;
  }
}

How do I configure nginx to add a slash? (I think i should a rewrite rule?)

9条回答
太酷不给撩
2楼-- · 2020-01-24 20:08

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;
   }
   # ...
}
查看更多
ら.Afraid
3楼-- · 2020-01-24 20:16

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;
}
查看更多
Summer. ? 凉城
4楼-- · 2020-01-24 20:16

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 / {
    ....
    }
}
查看更多
何必那么认真
5楼-- · 2020-01-24 20:18
rewrite ^([^.\?]*[^/])$ $1/ permanent;

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

e.g.

/myrest/do?d=12345

查看更多
Fickle 薄情
6楼-- · 2020-01-24 20:18

For nginx:

rewrite ^(.*[^/])$ $1/ permanent;
查看更多
Fickle 薄情
7楼-- · 2020-01-24 20:19

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

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

查看更多
登录 后发表回答