Similar Notes
Trailing slash redirect for Nuxt
updated:
23.09.2021
- Create middleware ~/middleware/trailingSlashRedirect.js with:
export default function ({ route, redirect }) {
if (route.path !== '/' && route.path.endsWith('/')) {
const { path, query, hash } = route;
const nextPath = path.replace(/\/+$/, '') || '/';
const nextRoute = { path: nextPath, query, hash };
redirect(nextRoute);
}
}
- Add this middleware for router section to nuxt.config.js.
export default {
…
router: {
middleware: ‘trailingSlashRedirect’
},
…
}