我有一套专门的重写规则来适应多站点 cms 设置。我试图让 nginx 在请求 URL 上强制使用尾部斜杠。我希望它重定向请求
domain.com/some-random-article 到 domain.com/some-random-article/
我知道这有语义方面的考虑,但我想出于 SEO 目的这样做。
这是我当前的服务器配置。
server {
listen 80;
server_name domain.com mirror.domain.com;
root /rails_apps/master/public;
passenger_enabled on;
# Redirect from www to non-www
if ($host = 'domain.com' ) {
rewrite ^/(.*)$ http://www.domain.com/$1 permanent;
}
location /assets/ {
expires 1y;
rewrite ^/assets/(.*)$ /assets/$http_host/$1 break;
}
# / -> index.html
if (-f $document_root/cache/$host$uri/index.html) {
rewrite (.*) /cache/$host$1/index.html break;
}
# /about -> /about.html
if (-f $document_root/cache/$host$uri.html) {
rewrite (.*) /cache/$host$1.html break;
}
# other files
if (-f $document_root/cache/$host$uri) {
rewrite (.*) /cache/$host$1 break;
}
}
我该如何修改它以添加尾部斜杠?我认为必须检查斜杠,这样你就不会得到 domain.com/some-random-article//
答案1
rewrite ^(.*[^/])$ $1/ permanent; # Capture everything not with a trailing slash and add a trailing slash to it.