How to remove trailing slash? #6584
-
I am using Remix 1.17 (all versions match) to deploy a site to Netlify. In my local testing and on Netlify I can access my routes with a trailing slash and without it. There is no redirect to a version without a slash. How do I configure this behavior? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
@kentcdodds' Epic Stack has a great explanation about this! 👀 https://github.com/epicweb-dev/epic-stack/blob/main/docs/redirects.md#remove-trailing-slashes |
Beta Was this translation helpful? Give feedback.
-
If you're using the Remix App Server instead of the Express adapter, here's how to do the same thing inside a Remix loader. Export this loader function from your export const loader = (async ({ request }) => {
const { pathname, search } = new URL(request.url);
if (pathname.endsWith('/') && pathname !== '/') {
// Redirect to the same URL without a trailing slash
throw redirect(`${pathname.slice(0, -1)}${search}`, 301);
}
return null;
}) satisfies LoaderFunction; |
Beta Was this translation helpful? Give feedback.
@kentcdodds' Epic Stack has a great explanation about this! 👀
https://github.com/epicweb-dev/epic-stack/blob/main/docs/redirects.md#remove-trailing-slashes