在每个网址的末尾添加斜杠(需要对nginx进行重写的规则) [英] Add slash to the end of every url (need rewrite rule for nginx)
问题描述
我尝试在每个网址末尾都添加一个"/":
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.
为此我需要重写规则.
为更好地理解,请检查以下内容:
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
与此伴随的另一种常见重写是:
Another common rewrite to accompany this would be:
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).
这篇关于在每个网址的末尾添加斜杠(需要对nginx进行重写的规则)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!