我尝试在每个网址结束时获得“/”:
example.com/art
应
example.com/art /
我使用nginx作为网络服务器。
我需要重写规则..
为了更好地理解,请查看:
http://3much.schnickschnack.info/art/projekte
如果你按下大图片下方的小缩略图,它会重新加载并显示此网址:
http://3much.schnickschnack.info/art/projekte/#0
如果我现在在所有网址上都有一个斜杠(最后),那么无需重新加载网站即可。
现在我在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以添加斜杠? (我想我应该重写规则?)
答案 0 :(得分:98)
更有可能我认为你会想要这样的东西:
rewrite ^([^.]*[^/])$ $1/ permanent;
正则表达式转换为: “重写所有URI而不使用任何'。'在它们中没有以'/'结尾的URI +'/'“ 或者干脆: “如果URI没有句点并且没有以斜杠结尾,请在末尾添加斜杠”
仅重写URI而没有点的原因使得任何具有文件扩展名的文件都不会被重写。例如你的图像,css,javascript等,并防止可能的重定向循环,如果使用一些自己的重写的PHP框架
伴随这种情况的另一个常见重写是:
rewrite ^([^.]*)$ /index.php;
这非常简单地将所有没有句点的URI重写到index.php(或者你执行控制器的任何文件)。
答案 1 :(得分:23)
rewrite ^([^.\?]*[^/])$ $1/ permanent;
避免使用/标记的其他网址的查询字符串。
e.g。
/ myrest /办?d = 12345
答案 2 :(得分:8)
对于nginx:
rewrite ^(.*[^/])$ $1/ permanent;
答案 3 :(得分:2)
奇怪这是谷歌的第一个结果,但没有一个满意的答案。我知道有两种很好的方法可以做到这一点。第一种是直接检查请求是否会命中一个文件,如果不是则只应用重写条件。 E.g。
server {
# ...
if (!-f $request_filename) {
rewrite [^/]$ $uri/ permanent;
}
location / {
# CMS logic, e.g. try_files $uri $uri /index.php$request_uri;
}
# ...
}
第二个,许多人更喜欢,因为他们宁愿避免使用,如果不是100%必需的话,就是使用try_files在获胜时将请求发送到命名的位置块没碰到文件。 E.g。
server {
# ...
location / {
try_files $uri $uri/ @cms;
}
location @cms {
rewrite [^/]$ $uri/ permanent;
# CMS logic, e.g. rewrite ^ /index.php$request_uri;
}
# ...
}
答案 4 :(得分:1)
在Wordpress中使用anthonysomerset的重写,我试验了由于重定向循环而访问/ wp-admin仪表板的问题。但我使用上述条件解决了这个问题:
if ($request_uri !~ "^/wp-admin")
{
rewrite ^([^.]*[^/])$ $1/ permanent;
rewrite ^([^.]*)$ /index.php;
}
答案 5 :(得分:1)
server {
# ... omissis ...
# put this before your locations
rewrite ^(/.*[^/])$ $1/ permanent;
# ... omissis ...
}
如果您希望阻止某些请求(比GET
之外的其他请求)这样做(通常是POST
个请求,因为rewrite
将任何请求方法转换为{ {1}},可能会破坏您网站的某些动态功能),添加GET
子句:
if
您也可以将server {
# ... omissis ...
# put this before your locations
if ($request_method = "GET" ) {
rewrite ^(/.*[^/])$ $1/ permanent;
}
# ... omissis ...
}
放在rewrite
区块(location
)中,以使其更具体。
答案 6 :(得分:0)
现在为时已晚,但我想分享我的解决方案,我遇到了尾随斜杠和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
这是我的解决方案
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 / {
....
}
}
答案 7 :(得分:0)
如果使用https代理nginx后,此代码段确实正确重定向$scheme
map $http_x_forwarded_proto $upstream_scheme {
"https" "https";
default "http";
}
server {
...
location / {
rewrite ^([^.\?]*[^/])$ $upstream_scheme://$http_host$1/ permanent;
}
...
}
在上游代理上传递X-Forwarded-Proto
标题,如:
location / {
proxy_set_header X-Forwarded-Proto $scheme;
...
}
答案 8 :(得分:-3)
试试这个:^(.*)$ http://domain.com/$1/ [L,R=301]
重定向(状态代码301)所有内容($ 1)没有“/”到“$ 1 /”