在每个 url 的末尾添加斜杠(需要为 nginx 重写规则) [英] Add slash to the end of every url (need rewrite rule for nginx)

查看:27
本文介绍了在每个 url 的末尾添加斜杠(需要为 nginx 重写规则)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在每个 url 结尾处添加一个/":

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

example.com/art

example.com/art

应该

example.com/art/

example.com/art/

我使用 nginx 作为网络服务器.

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

如果我现在在所有 url 上都有一个斜杠(在最后),它会在不重新加载网站的情况下工作.

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

现在我在 nginx-http.conf 中有这个设置:

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;
  }
}

如何配置nginx添加斜线?(我认为我应该重写规则?)

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

推荐答案

我认为你更有可能想要这样的东西:

More likely I think you would want something like this:

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

正则表达式转换为:"重写所有 URI,不带任何 '.'其中不以 '/' 结尾的 URI + '/'"或者干脆:"如果 URI 没有句点且不以斜线结尾,则在末尾添加斜线"

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"

只重写其中没有点的 URI 的原因使得任何具有文件扩展名的文件都不会被重写.例如,您的图像、css、javascript 等,如果使用一些自己重写的 php 框架也可以防止可能的重定向循环

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

另一个常见的重写是:

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

这非常简单地将所有没有句点的 URI 重写到您的 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).

这篇关于在每个 url 的末尾添加斜杠(需要为 nginx 重写规则)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆