0

I moved my website and I have a QR code (which is printed in public and can't be easily replaced) that points to a specific file on my old website that has now been moved. Currently, the URL just points to a "Not found" page on my new website. I try to use javascript in the header to catch the URL and forward it to the right URL as following:

<script type="text/javascript">
    if(window.location.href === "https://www.website.com/multimedia/hoerproben/1.mp3")
    {
        window.location.href = "https://www.webseite.com/app/download/10079133850/1.mp3";
    }
</script>

But it doesn't work. Any hints what I am doing wrong?

2
  • Maybe instead of doing it on the client, make the route on the server respond with 301 Moved Permanently to your new address.
    – 101arrowz
    May 31, 2020 at 7:39
  • wait a second. are you trying to do it client side? It just can't be done - JavaScript inside a webpage has (almost) no power on subpages May 31, 2020 at 7:39

2 Answers 2

0

when you open an url, the browser makes an http request to your server for that particular resource (in your example, an mp3 file). JavaScript is not involved at all (actually, there are so called "service workers", but they are not what you're looking for, they are meant to do caching, not redirecting). The browser does not know that your JavaScript code exists and would not execute it.

What you should do is route redirecting from server, so when the browser asks from /oldlocation/file.mp3, instead the server answers with /newlocation/file.mp3

This could be in some different way according to your server. If you have no control on how your server works, what you're asking is simply not possibile.

0

It won't work unless you place that code in the "Not found" page that gets served. If your URL pointed to an HTML file, you could have just placed one to do the redirect. For media files you would have to configure your server to serve an HTML file instead. Don't worry about the extension, it's the Content-Type header that determines the type of the file served. Doing this, however, is not good practice because your server would still be returning a 200 response code.

It's good practice to return 301 Moved Permanently as 101arrowz pointed out in the comments. How that can be accomplished will depend on what server you're using.

Here's how that would have been accomplished with express.js:

app.get('/multimedia/hoerproben/1.mp3', function(req, res) {
  res.redirect('/app/download/10079133850/1.mp3');
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.