Add trailing slash if one doesn't exist

Is it possible to add a trailing slash if one doesn’t exist when a request comes in? I have a reverse_proxy and the upstream requires a trailing slash.

Yep! The php_fastcgi directive’s expanded form has an example of this:

@canonicalPath {
	file {
		try_files {path}/index.php
	}
	not path */
}
redir @canonicalPath {path}/ 308

It’s up to you to modify this to work for your use-case. The file matcher won’t be what you need, but I’m not certain in what conditions you’d need this because you didn’t give any examples. Please elaborate next time so we can help you more specifically!

Here’s my example

{
  "apps": {
    "http": {
      "servers": {
        "myserver": {
          "listen": [
            ":443"
          ],
          "routes": [
            {
              "handle": [
                {
                  "body": "Hello World!",
                  "handler": "static_response"
                }
              ],
              "match": [
                {
                  "host": [
                    "domains.mylodocs.com"
                  ]
                }
              ]
            },
            {
              "handle": [
                {
                  "body": "Hello World!",
                  "handler": "static_response"
                }
              ],
              "match": [
                {
                  "host": [
                    "admin.mylodocs.com"
                  ]
                }
              ]
            },
            {
              "handle": [
                {
                  "handler": "rewrite",
                  "uri": "/{http.request.host.labels.2}/error.html?{http.request.uri.query}"
                },
                {
                  "encodings": {
                    "gzip": {
                      "level": 0
                    }
                  },
                  "handler": "encode"
                },
                {
                  "handler": "reverse_proxy",
                  "headers": {
                    "request": {
                      "set": {
                        "Host": [
                          "mylodocs.s3-website-us-west-2.amazonaws.com"
                        ]
                      }
                    }
                  },
                  "upstreams": [
                    {
                      "dial": "mylodocs.s3-website-us-west-2.amazonaws.com:80"
                    }
                  ]
                }
              ],
              "match": [
                {
                  "host": [
                    "*.mylodocs.com"
                  ],
                  "path": [
                    "/check"
                  ]
                }
              ]
            },
            {
              "handle": [
                {
                  "handler": "rewrite",
                  "uri": "/{http.request.host.labels.2}{http.request.uri}"
                },
                {
                  "encodings": {
                    "gzip": {
                      "level": 0
                    }
                  },
                  "handler": "encode"
                },
                {
                  "handler": "reverse_proxy",
                  "headers": {
                    "request": {
                      "set": {
                        "Host": [
                          "mylodocs.s3-website-us-west-2.amazonaws.com"
                        ]
                      }
                    }
                  },
                  "upstreams": [
                    {
                      "dial": "mylodocs.s3-website-us-west-2.amazonaws.com:80"
                    }
                  ]
                }
              ],
              "match": [
                {
                  "host": [
                    "*.mylodocs.com"
                  ]
                }
              ]
            },
            {
              "@id": "11",
              "handle": [
                {
                  "handler": "rewrite",
                  "uri": "/mdennett/masonite-framework{http.request.uri}"
                },
                {
                  "encodings": {
                    "gzip": {
                      "level": 0
                    }
                  },
                  "handler": "encode"
                },
                {
                  "handler": "reverse_proxy",
                  "headers": {
                    "request": {
                      "set": {
                        "Host": [
                          "mylodocs.s3-website-us-west-2.amazonaws.com"
                        ]
                      }
                    }
                  },
                  "upstreams": [
                    {
                      "dial": "mylodocs.s3-website-us-west-2.amazonaws.com:80"
                    }
                  ]
                }
              ],
              "match": [
                {
                  "host": [
                    "docs.mitchelldennett.com"
                  ]
                }
              ]
            }
          ]
        }
      }
    },
    "tls": {
      "automation": {
        "on_demand": {
          "ask": "https://app.mylodocs.com/domains/ask"
        },
        "policies": [
          {
            "on_demand": true
          }
        ]
      }
    }
  }
}

for the route that matches only on *.mylodocs.com I want to add a trailing slash if one doesn’t exist

            {
              "handle": [
                  {
                      "handler": "subroute",
                      "routes": [{
                          "match": [{
                              "not": [{
                                  "path": ["*/"]
                              },
                              {
                                  "path": ["*.html", "*.css", "*.js", "*.png", "*.svg"]
                              }]
                          }],
                          "handle": [{
                            "handler": "static_response",
                            "headers": {"Location":["{http.request.uri}/"]},
                            "status_code": 308
                          }]
                      },
                      {
                          "match": [{
                              "path": ["*/"]
                          },
                          {
                                "path": ["*.html", "*.css", "*.js", "*.png", "*.svg"]
                          }],
                          "handle": [
                            {
                                "handler": "rewrite",
                                "uri": "/{http.request.host.labels.2}{http.request.uri}"
                            },
                            {
                                "encodings": {
                                    "gzip": {
                                    "level": 0
                                    }
                                },
                                "handler": "encode"
                            },
                            {
                                "handler": "reverse_proxy",
                                "headers": {
                                    "request": {
                                        "set": {
                                            "Host": [
                                                "mylodocs.s3-website-us-west-2.amazonaws.com"
                                            ]
                                        }
                                    }
                                },
                                "upstreams": [{
                                    "dial": "mylodocs.s3-website-us-west-2.amazonaws.com:80"
                                }]
                            }
                          ]
                      }]
                  }
              ],
              "match": [
                {
                  "host": [
                    "*.mylodocs.com"
                  ]
                }
              ]
            }

This is what I was after. Posting here if any else has the same problem

1 Like

"path": ["*.html", "*.css", "*.js", "*.png", "*.svg"]

I’m doing this because I don’t want to put a / after a file. such as styles.css.

I’m not super happy about this. My path has 1.0 in it. so i can’t do *.*. Anyway around this?

You could use path_regexp instead and only match on [A-Za-z] after the .

You can use https://regex101.com/ with the Golang option on the left sidebar to help you with crafting your regex!

I thought I read somewhere that Go regex’s are pretty slow. Is using a regex going to slow down matching that much?

It’ll be basically imperceptible. The regexes are compiled once at load time, then executed on every request, so it’s not that bad.

It also completely depends on the regexp itself. It depends how many operations you make.

Are they slower than substring checks? Yes.

Is it going to cause a noticeable issue? Almost certainly not, unless you have a very hot website and need to optimize every request as much as technologically possible.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.