Skip to content
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

Closed
AGrinx opened this issue Sep 6, 2017 · 10 comments
Closed

Redirects for SEO #1592

AGrinx opened this issue Sep 6, 2017 · 10 comments
Labels

Comments

@AGrinx
Copy link

AGrinx commented Sep 6, 2017

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 :-)

This question is available on Nuxt.js community (#c1421)
@awronski
Copy link

awronski commented Sep 6, 2017

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

@AGrinx
Copy link
Author

AGrinx commented Sep 6, 2017

Thanks, it is a good idea, but maybe there is more nuxt-way solution?
Because I have only my app and the api.. ((

@dohomi
Copy link

dohomi commented Sep 7, 2017

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.

@NicoPennec
Copy link

@AngelWayfarer

If you really want a pure Nuxt solution, you can use a serverMiddleware

doc: https://nuxtjs.org/api/configuration-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 ;-)

  1. Create a list of 301 redirections in a json file at project root:
// 301.json 

[
  { "from": "/old", "to": "/new" },
  { "from": "/veryold", "to": "/verynew" },
  { "from": "/too-old", "to": "/new" }
]
  1. Create a server middleware for 301 redirect
// 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()
  }
}
  1. Add the server middleware in your config file:
// nuxt.config.js

module.exports = {
  serverMiddleware: [
    '~/servermiddleware/seo.js'
  ],
 ...
}
  1. Run your server & enjoy 👍

@AGrinx
Copy link
Author

AGrinx commented Sep 12, 2017

@NicoPennec Thanks for your usefull answer!!!
I will try it soon.
Maybe you can help me with another problem.
I had two apps.
Nuxt frontend + Laravel Backend(API + Admin-panel)

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 want to use paths without domain name like '/images/myImage.jpg'
And show thee image stored in http://MYAPPNAME.com/uploads/myImage.jpg

I think that it can be with some magic with the serveerMiddleware :-)

Thanks for your answer :-)

@NicoPennec
Copy link

For this kind of stuffs, I prefer use a proxy from Nginx / Apache /...

But, Nuxt ServerMiddleware is based on the Connect project.
And Connect has a Server-Static handler from Express team.
You can try it.

An extract from Nuxt doc:

https://nuxtjs.org/api/configuration-servermiddleware#usage 😉

// nuxt.config.js

const serveStatic = require('serve-static')

module.exports = {
  serverMiddleware: [
      { path: '/static2', handler: serveStatic(__dirname + '/static2') }
  ]
}

@AGrinx
Copy link
Author

AGrinx commented Sep 12, 2017

Thanks a lot!!!
I found it in the doc, but I was not sure if it be suitable for my problem. But if you say that it can work, so I will read the docs more deep 😉

@AGrinx AGrinx closed this as completed Sep 12, 2017
@NicoPennec
Copy link

I've never played with the server middleware before your issue.
I wanted to test it because I thought you had a very relevant question 👍
Massive redirective 301 is very common for SEO ;-)

@MauricioHernanCabrera
Copy link

How can I do with dynamic routes?

@lock
Copy link

lock bot commented Nov 1, 2018

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.

@lock lock bot locked as resolved and limited conversation to collaborators Nov 1, 2018
@danielroe danielroe added the 2.x label Jan 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

7 participants