location / {
    # try to serve file directly, fallback to rewrite
    try_files $uri @rewriteapp;
}

location @rewriteapp {
    # Redirect .well-known urls (https://en.wikipedia.org/wiki/List_of_/.well-known/_services_offered_by_webservers)
    rewrite .well-known/carddav /dav/ permanent;
    rewrite .well-known/caldav /dav/ permanent;
    rewrite .well-known/security.txt$ /security.txt permanent;

    # Old carddav url
    rewrite carddav/(.*) /dav/$1 permanent;

    # rewrite all to app.php
    rewrite ^(.*)$ /index.php/$1 last;
}

location ~ ^/(app|app_dev|config)\.php(/|$) {
    try_files @heroku-fcgi @heroku-fcgi;
    internal;
}