Can I strip .pdf extension in redirect rule?

Netlify Site Name: https://fervent-engelbart-aff508.netlify.app/

I would like to redirect this /dokument/produktblad/:product_code.pdf where :product_code is something like ABC-123 to https://partnerzon.specialelektronik.se/redirect/product/:product_code

/dokument/produktblad/ABC-123.pdf would redirect to https://partnerzon.specialelektronik.se/redirect/product/ABC-123

Is that possible?

Thanks!

It’s possible if there exists a HTML page at https://partnerzon.specialelektronik.se/redirect/product/ABC-123. If you’re just looking to hide .pdf extension, then it’s not possible. You’d have to generate a HTML page which will embed the PDF in it or something of that sort.

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

Thank you @jonsully! That will do the trick!

1 Like