Redirect whole site to subfolder (except static assets like CSS/JS)

Hi!

So we’re currently in the process of making our site multi-lingual. Our plan is to move to sub-folder based language-urls: my-site.com becomes my-site.com/en, my-site.com/cool-stuff becomes my-site.com/en/cool-stuff, etc.

This more or less works if we add language-based redirects to our Netlify config:

/*  /en/:splat Language=en

But now everything gets redirected (incl. CSS and JavaScript files which live in the root of the website and aren’t localized).

Is there a way to get around this?

Thanks in advance.

Flo

Hiya, @flo!

While in theory that would work as you’re intending since /asset.png would be an existing file and you don’t use the syntax to force redirection even when it would shadow a file (cf https://www.netlify.com/docs/redirects/#note-on-shadowing), in practice, language redirects do behave as though they were forced. While this seems to be as designed even though it feels to me like a bug, it makes your use case a bit harder to accomplish

So, you have a few options I can think of:

  1. decouple your assets from your main site (have e.g. a “assets.mysite.com” under which you serve all assets). This could be as simple as a second deploy of your code with a different base directory (/assets) or even just a straight second deploy WITHOUT that redirect to which you could link at the same path (<img src=/asset.png> becomes <img src=https://assets.mysite.com/asset.png>)
  2. duplicate your assets into each language directory. Inelegant, but if you have only a handful, might be simpler.

I am sure there are other options but those are the two I could come up with easily.

I have attached this thread to the feature request to improve the handling of language redirects to NOT always act as forced, so I can follow up here in case that feature changes behavior.

Hi @fool,

Thanks for your response. We went with your second suggestion and it seems to work fine. But just out of curiousity, have you already fixed the shadowing issue? Because I tried it on a test-environment and it seems to work without the work-around.

Best,
Flo

It’s not fixed, but it isn’t consistently broken either in our testing - a lot has to do with whether anyone has already visited the site on a CDN node since last deploy - we’ll often cache the first thing that comes through. If the first request is for https://yourlanguagegatedsite.com/asset.png that will likely work, but if it’s for a path that would require a redirect, that redirect may get cached instead.

The fix you’ve implemented will work either way - whether it happens to work as you’re hoping, or not :slight_smile: