-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Redirects for SEO #1592
Comments
Use Nginx before nuxt instance. You can use cron to create redirect rules every x minuts or use some more sophisticated solution like lua+redis |
Thanks, it is a good idea, but maybe there is more nuxt-way solution? |
I extended router prop inside of nuxt.config.js like following: router: {
extendRoutes (routes, resolve) {
routes.push({
name: 'page',
path: '*',
component: resolve(__dirname, 'src/pages/index.vue')
})
}
}, Every request which is not handled by the folders of Nuxt are then send down to index.vue and there I handle each request - forward them or simply redirect to the 404 page. You can handle the logic either in fetch({route}) or on created hook - thats up to you. Not sure if this solution covers your usecase as well. |
@AngelWayfarer If you really want a pure Nuxt solution, you can use a serverMiddleware In the following example, I will create a simple json file with all 301 redirections, but you can replace it by a CSV or an API call ;-)
// 301.json
[
{ "from": "/old", "to": "/new" },
{ "from": "/veryold", "to": "/verynew" },
{ "from": "/too-old", "to": "/new" }
]
// servermiddleware/seo.js
const redirects = require('../301.json')
module.exports = function (req, res, next) {
const redirect = redirects.find(r => r.from === req.url)
if (redirect) {
console.log(`redirect: ${redirect.from} => ${redirect.to}`)
res.writeHead(301, { Location: redirect.to })
res.end()
} else {
next()
}
}
// nuxt.config.js
module.exports = {
serverMiddleware: [
'~/servermiddleware/seo.js'
],
...
}
|
@NicoPennec Thanks for your usefull answer!!! When the admin/content manager uploads some files or images from the admin-panel - they save into /public/uploads/ folder in the laravel app. How can I serve all the static images?? I think that it can be with some magic with the serveerMiddleware :-) Thanks for your answer :-) |
For this kind of stuffs, I prefer use a proxy from Nginx / Apache /... But, Nuxt ServerMiddleware is based on the Connect project. An extract from Nuxt doc:
// nuxt.config.js
const serveStatic = require('serve-static')
module.exports = {
serverMiddleware: [
{ path: '/static2', handler: serveStatic(__dirname + '/static2') }
]
} |
Thanks a lot!!! |
I've never played with the server middleware before your issue. |
How can I do with dynamic routes? |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Hi, I have a big problem...
I am developing now an e-shop using nuxt and I stuck at the redirections moment.
We have the laravel backend with api and admin-panel, so our SEO department can use SEO module to write a big array of redirects for they needs. All this data stores in the DB.
And I am confused for the right solution to implement these redirects into my SSR+SPA App
Do you have any ideas for the most beautiful solution for this problem?
Thanks for any answer :-)
The text was updated successfully, but these errors were encountered: