Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

260
Visualizações
when i try to redirect to decoded url it will redirect me to my myurl.com/myurl instead taking me to the page

I'm making a link shortener. I previously got a problem with the URLs but it got fixed with encoding the URL when putting it in the database and when redirecting it will decode the URL and redirect to it. The problem is that instead of redirecting me to like https://google.com it redirects me to mypage.com/google.com. I tried making a "debug" page when it just decode the URL and the URL is fine, with HTTPS:// and everything. The biggest problem is that it's all working on localhost but when I deploy it on my VPS it's not working. Only the debug page that decodes the URL works. I'm using express.js and mongoose. Here's my code for redirecting users:

    app.get('/:shortUrl', async (req, res) => {
         const shortUrl = await shorturl.findOne({ short: req.params.shortUrl })
    if (shortUrl == null) {
        res.send('URL not found!')
    } else {
        shortUrl.clicks++
        shortUrl.save()
        res.redirect(decodeURIComponent(shortUrl.full))
    }
})
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can use the built in URL object in order to make sure the redirect URL is full and valid:

res.redirect(new URL(decodeURIComponent(shortUrl.full)).toString())

If it is unable to make a valid URL from the input, it will throw, so it's better to wrap it in a try/catch.

try {
  res.redirect(new URL(decodeURIComponent(shortUrl.full)).toString());
} catch (e) {
  res.send('Invalid URL');
}
about 4 years ago · Juan Pablo Isaza Relatório

0

If the URL does not have a scheme, in a browser it assumes the scheme is HTTP, but in HTTP redirects the domain looks like a path with a dot. If you redirect to a path, it will redirect on the same domain, which explains the behavior with google.com.

Try normalizing the URL or validating the full URL includes the scheme.

https://github.com/sindresorhus/normalize-url

https://developer.mozilla.org/en-US/docs/Learn/Common_questions/What_is_a_URL

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda