Netlify redirects file

I’m deploying a v4 site to Netlify and am trying to include a Netlify _redirects file, which of course is ignored by Middleman. I tried manually copying the file during build, but it gets removed before build finishes.

Any ideas?

Try to ignore them, additionally to manually copy them during build.
https://middlemanapp.com/advanced/dynamic-pages/#ignoring-files

You can use a proxy to rename a netlify_redirects file (or whatever name you want) to _redirects on build: https://github.com/middleman/middlemanapp.com/blob/master/config.rb#L27

Thanks for the replies.

@tysongach That worked like a charm.

Sorry, why you need to use netlify redirects with Middleman?

I’m putting in production my site in these days with Middleman / Netlify / NetlifyCMS so I’m curious to see if I can find problems

I redesigned a site and wanted to preserve the old URLs so visitors wouldn’t get 404 pages.

It is not possible set this in Middleman, like for me:

config.rb

redirect “index.html”, to: “/it/index.html”

Yes, it is possible to use redirect in Middleman. But, that only creates an html file with a refresh meta tag:

It’s better to let Netlify do a proper 301 redirect for search optimization purposes. There is a warning in the Middleman docs:

https://middlemanapp.com/basics/redirects/

1 Like

Unfortunately, the proxy option doesn’t work anymore. I was able to solve this using the after_build hook as described on this stack overflow question: https://stackoverflow.com/questions/23879068/middleman-run-custom-action-after-build

@susodapop proxy works just fine, I just implemented this exact same method yesterday:

ready do
  proxy "_redirects", "netlify-redirects", ignore: true
end

Thank you! I tried your syntax and it works for me too. Ruby is not my primary language and none of the docs include the ready do ... end block. Not even the example from @tysongach above. The proxy worked as soon as I added it. Could you explain what that ready do ... end block means?

@susodapop ready is a lifecyle event just like after_build, it basically runs code once Middleman is ready to serve or build files.

1 Like