<p>I'm trying to deploy a static website to a Kubernetes cluster which is using the official Nginx Ingress controller. The folder structure of the website looks somewhat like this:</p>
<pre><code>/
├── about
│   └── index.html
├── casestudy
│   ├── data-and-analytics
│   │   └── index.html
│   └── workflow-automation
│       └── index.html
├── contact
│   └── index.html
├── css
│   ├── font-awesome.min.css
│   ├── fonts
│   │   ├── slick.eot
│   │   ├── slick.svg
│   │   ├── slick.ttf
│   │   └── slick.woff
│   ├── footer.css
│   ├── layout.css
...
</code></pre>
<p>My ingress definition looks like this:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: website-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    # nginx.ingress.kubernetes.io/rewrite-target: /$2
    cert-manager.io/cluster-issuer: letsencrypt
spec:
  tls:
    - hosts:
        - website.com
      secretName: website-tls
  rules:
    - host: website.com
      http:
        paths:
          - path: /
            backend:
              serviceName: website-svc
              servicePort: 8080
</code></pre>
<p>This works fine for the most part, except that if I forget to put a trailing slash at the end of the URL like <code>https://website.com/about</code> I get routed into an error page. I understand why this is happening - Nginx is looking for a <code>about.html</code> file and is failing to find one. But I don't know how to fix this.</p>
<p>What I'd ideally like to do is that I want to add a trailing <code>/</code> to requests which don't have one. But I also want to not do this when the browser is requesting for a css file.</p>
<p>What redirect annotation and rule should I use for this?</p>
<p>Thanks.</p>

<p>What ultimately worked for this situation is a snippet like this:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: website-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    cert-manager.io/cluster-issuer: letsencrypt
    nginx.ingress.kubernetes.io/rewrite-target: /
    # Rewrite all URLs not ending with a segment containing . or ? with a trailing slash
    # So basically we are rewriting all folder names with a trailing slash.
    nginx.ingress.kubernetes.io/configuration-snippet: |
      rewrite ^([^.?]*[^/])$ $1/ redirect;
spec:
  tls:
    - hosts:
        - website.com
      secretName: website-tls
  rules:
    - host: website.com
      http:
        paths:
          - path: /
            backend:
              serviceName: website-svc
              servicePort: 8080
</code></pre>
<p>This will let us rewrite all URLs ending with a segment containing no <code>.</code> <em>(period - thus avoiding filenames)</em> and <code>?</code> <em>(question mark - thus avoiding all query strings)</em> with a trailing slash. This works for my case.</p>
