Any chance of dynamic redirects?

I’m wondering, what is the status of dynamic redirects? I see that the idea has been proposed in a few places:

I have my own use case for which this would come in very handy: I want to redirect all URLs of the form /blog/2010/4/title.html to /blog/2010/04/title.html (i.e. adding the leading zero in single-digit months), and it needs to be an external redirect to appease Google and to hopefully reduce how often people hit these old URLs in the future. Is there any plan for this actually being implemented within Caddy?

The discussion on this seems kind of fragmented but I wasn’t sure where to take it, whether I should make yet another comment on one of the above linked posts or not. I will be happy to move this discussion to an existing topic if that would help consolidate things.

And here’s another case where I really need dynamic redirects: redirecting multiple subdirectories to another domain. For example, suppose I want to redirect everything under example.com/files to files.example.com (keeping the same URL path) and everything under example.com/images to static.example.com (again keeping the same URL path). I can do one of these with

redir {
    if {path} starts_with /files
    / {scheme}://files.example.com{uri}
}

but if I then add the corresponding configuration section for images,

redir {
    if {path} starts_with /images
    / {scheme}://static.example.com{uri}
}

I get caddy complaining about a rule with duplicate 'from' value. But I can’t change the from value without losing the catch-all functionality and making the redirects useless.

Dynamic redirects would make it so I could specify from values that match more than one specific path, and presumably would solve this problem.

I really hope this gets implemented at some point, otherwise I may have to jump ship to another server. (I’d work on it myself if I knew Go, but I don’t, nor do I see myself having time to learn in the near future.)

Hey @diazona, thanks for requesting this! Since this is important to you, would you please open an issue to request the feature? You can link to this forum thread and/or copy+paste the details into the issue. (You don’t have to follow the bug report template for feature requests.) Feature requests on the issue tracker are less likely to be forgotten or lost.

1 Like

Absolutely, I will do that today. Thanks for responding!

(edit) Now submitted as issue 1749

https://github.com/mholt/caddy/issues/1749

In case it helps other people reading this thread later, here are the workarounds I implemented for now:

  • For fixing the single-digit month numbers, I wrote a script that prints out the incorrect and correct URLs for all my blog posts, like so:

      ...
      /blog/2011/2/it-s-a-liiiiiive.html /blog/2011/02/it-s-a-liiiiiive.html
      /blog/2011/2/why-you-slip-on-ice-and-how-to-avoid-it.html /blog/2011/02/why-you-slip-on-ice-and-how-to-avoid-it.html
      /blog/2011/3/ignoring-project-files-in-git.html /blog/2011/03/ignoring-project-files-in-git.html
      ...
    

    Then I wrap those in a redir { ... } block and import it into my Caddyfile. It’s not as convenient as I would have liked but it does work - Caddy has no problem handling hundreds of redirects.

  • For the subdirectory redirects, my workaround was to declare new configuration blocks in the Caddyfile. For example if my original configuration was

      example.com {
          redir {
              if {path} starts_with /files
              / {scheme}://files.example.com{uri}
          }
      }
    

    I’d change that to

      example.com {
      }
    
      example.com/files {
          redir / {scheme}://files.example.com{uri}
      }
    

    and I can then add another section for /images like so:

      example.com/images {
          redir / {scheme}://static.example.com{uri}
      }
    

    Caddy doesn’t complain about duplicate redirect sources with this. But of course this technique only works for redirecting based on a path prefix, it’s not a general solution for dynamic redirects.

3 Likes

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