Redirecting based on file extension only

Hi! I’m trying to get my site to redirect RSS requests based on the file extension. This is the redirect rule I’ve got in my _redirects file:

/:tag.rss  /tags/:tag.rss

The problem is that it’s matching on any URL, including those that don’t end in .rss. For example, /foo redirects to /tags/foo.

I’m migrating from an old CMS that had RSS feeds at e.g. /foo.rss and they need to be redirected to /tags/foo.rss.

Hi, @samwilson, I think you are expecting a placeholder named :tag which matches <insert placeholder here >.rss. Instead the placeholder is :tag.rss and it matches any single path segment URL.

For these paths:

  • /foo.rss
  • /foo.css
  • /foo.html

This redirect will match all of everything after the “/” and store it in a placeholder named :tag.rss. This is what will match of each one above:

  • foo.rss
  • foo.css
  • foo.html

A placeholder will match everything between two “/” characters or from a “/” to the end of the path (up to but not including any GET parameters in the URL). It matches a whole “path segment”.

Take the URL below as an example:

https://freosam.netlify.com/path/to.some/page.rss?something=foo&otherthing=bar

The path of the URL above is:

/path/to.some/page.rss

The are three path segments which are:

  • path
  • to.some
  • page.rss

The redirect rule makes a placeholder named :tag.rss which matches anything with has only the initial “/” and no other path segments. It does not match URLs with more than one path segment.

For this URL:

https://freosam.netlify.com/foo

The placeholder named :tag.rss is matching the first path segment which is the “foo” in /foo. Then on the target side of the redirect, the placeholder of :tag.rss is replaced with foo and redirecting to /tags/foo.

To summarize, the placeholder cannot match only a portion of a path segment, it must match the whole segment or nothing.

1 Like

Came here trying to figure out if I could leverage a placeholder to match something before an image extension, i.e.

I want to redirect /people/luke.png to https://domain.com/luke/128.png

But it sounds like this is not possible because a placeholder only matches what’s between / marks:

# this will redirect `/people/luke.png` to `https://domain.com/luke.png/128.png`
/people/* https://domain.com/:splat/128.png

# this is kind of what I would hope for, but wouldn't work
/people/:luke.png https://domain.com/:luke/128.png

@luke’s observation is very useful:

A placeholder will match everything between two “/” characters or from a “/” to the end of the path (up to but not including any GET parameters in the URL)

So useful, that I could’ve skipped scouring the community if the “Placeholders” docs had stated this. Might be useful to add that bit of clarity in the docs?

Howdy @jimniels! We have filed a feature request for this and linked this thread to it. If we end up implementing this functionality, we will let you know!

1 Like