2

How can I redirect all https://www.A.net/Page.html requests for some page Page.html to the corresponding page on another domain https://www.B.net/Page.html via the https://www.A.net/404.html? Github/Gitlab Pages redirect all Page-not-found errors to the latter. Is it possible to somehow retrieve the original requested page and use this in a Javascript function to modify the redirection URL?

I currently use something like the following HTML code for a many-to-one redirection, but I rather need a one-to-one redirection (i.e. not always to the same https://www.B.net/404.html).

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8"/>
        <meta http-equiv="refresh" content="0; URL=https://www.B.net/404.html">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
        <meta name="viewport" content="width=device-width; initial-scale=1.0"/>
    </head>
    <body>
        You are being redirected to https://www.B.net/404.html <a href="https://www.B.net/404.html">https://www.B.net/404.html</a>
    </body>
</html>

Current situation (many-to-one):

www.A.net/Page1.html -> www.A.net/404.html -> www.B.net/404.html
www.A.net/Page2.html -> www.A.net/404.html -> www.B.net/404.html

Desired situation (one-to-one):

www.A.net/Page1.html -> www.A.net/404.html -> www.B.net/Page1.html
www.A.net/Page2.html -> www.A.net/404.html -> www.B.net/Page2.html

Note that I host static websites at Github/Gitlab.

Specific context: I want to redirect my Gitlab and Bitbucket Pages to my Github Pages.

yivi
  • 42,438
  • 18
  • 116
  • 138
Matthias
  • 4,481
  • 12
  • 45
  • 84

2 Answers2

2

You could access the referrer via Javascript, parse it there and redirect accordingly.

E.g.

<script>
     let referrer = new URL(document.referrer);
     let hostRedirection = 'http://exampleB.com';

     document.location.href = hostRedirection + referrer.pathname;
</script>

Since I'm using URL, this is not IE compatible (but it is compatible with Edge), although it is very terse and readable. Doing it with a regexp shouldn't be too hard.

Adding some error handling in case the referrer wasn't set would be strongly advisable.

yivi
  • 42,438
  • 18
  • 116
  • 138
  • In Chrome and Firefox, I obtain a `TypeError` for the `URL` construction, since `document.referrer` is `""`. Though, I guess this is the behavior when you manually type the path instead of click on a link. – Matthias Jul 04 '18 at 19:06
  • @Matthias If you write the link yourself, you obviously **do not have a referrer**. – yivi Aug 22 '18 at 09:48
  • I know, but I want to handle that case as well (i.e. redirection from a href + manual writing). :-) – Matthias Aug 22 '18 at 13:41
  • @Matthias it is as simple as writing a check to see if referrer wasn't set. That's what I suggested in the last line of my answer. Good luck! – yivi Aug 23 '18 at 07:17
0

You can't do this on purely Javascript level. It is because Javascripts, running on the client side, have no access to the browser history on security reasons.

If you are on the server side, you can use a Referer: HTTP-Header on the code generating www.A.net/404.html, to generate such a 404 Page, which redirects to your desired destination.

Note, such things are considered clearly worse solution, if you do them on the client side. They are slower, they are making your site Javascript-dependent, also crawlers won't be able to follow it (if they aren't enough smart to execute the javascripts of the site), and so on.

Better if you learn some server-side programming language/framework and do with it; although also pure client solutions are quite common.

peterh
  • 11,875
  • 18
  • 85
  • 108
  • In another [Q&A](https://stackoverflow.com/a/36301574/1731200), I read something about `window.location` containing the not-found URL. Not sure what this concretely means and if this is something that is only applicable to Github Pages. – Matthias Jul 04 '18 at 16:37
  • 1
    @Matthias There are various tricks for that, for example you can make the 404.html dependent from the original url (for example, to `404.html?url=Page1.html`) which makes you possible to read the original URL from your `window.location`. But all of them requires some server-side tricks. What is sure: the previously visited URLs are hidden for the client-side javascripts, it doesn't matter what you do. You need to find some server-side trick to pass this info to them. – peterh Jul 04 '18 at 16:42
  • 1
    [document.referrer](https://developer.mozilla.org/en-US/docs/Web/API/Document/referrer): `[...]Returns the URI of the page that linked to this page.[...]`. But both the JavaScript `document.referrer` or the `Refere` header might not be set or might only contain partial informations depending on the browser settings. @Matthias – t.niese Jul 04 '18 at 16:44
  • 1
    @t.niese It never worked by me. In the ancient times, even `window.history` was available for client-side JS, now only `history.back()` is working. How old is your doc? – peterh Jul 04 '18 at 16:45