Can I strip .pdf extension in redirect rule?

Greetings @carlfredrikhero! :wave:t2:

Welcome to The Community :netliheart:

I don’t believe you can do what you’re looking to do directly in a _redirect but you absolutely can redirect (or ‘rewrite’ as we call it when you use a 200 status) to a Function that you write, which would return a 302 (or 301 if you prefer) to the un-.pdf’d location. Effectively encoding some kind of logic like:

# _redirects
/dokument/produktblad/* /.netlify/functions/custom-redirector 200
// custom redirector Function
exports.handler = async (event, context) => {
  const targetPath = event.path
  if (targetPath.match(/.pdf$/) {
    return ({
      statusCode: 302
      headers: {
        Location: /* path without .pdf */
      }
    })
  }
}

but you will need to be careful about how redirect/rewrite shadowing works since you’re going to be redirecting into the same directory that the * splat targets. Check out these docs for more info, but hopefully this gives you a place to start.


Jon

1 Like