Reputation: 4677
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.
Upvotes: 2
Views: 1738
Reputation: 47308
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.
Upvotes: 2
Reputation: 12575
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.
Upvotes: 0