Redirect domain to another domain

Started by
1 comment, last by frob 5 years, 9 months ago

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 redirects all Page-not-found errors to the latter. Is it possible to somehow retrieve the original requested page and use this in a Javascript (jQuery) 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>

 

🧙

Advertisement

There are many ways, as many as your creativity allows.

If you want the entire domain redirected to a different server, you might be able to handle it with a DNS entry and a line in your web server to treat one domain as the other.  That is, set the IP address of a.com to be the same as b.com and allow the server for b.com to acknowledge a.com as the site's name.

A more typical approach is the HTTP server redirect. This requires code on your server. Normally a page gives a 200 OK response.  The 404 page you described usually isn't just the text, the server also sends a 404 NOT FOUND response code. Redirection response codes are 301 MOVED PERMANENTLY or a 307 TEMPORARY REDIRECT which will bounce a web browser to the new location. There is also the 303 SEE OTHER response which is usually handled after a POST, PUT, or DELETE and tells the browser to issue a GET request at that other page.  Exactly how you do this depends on how you create your pages.  If you watch a web debugger on large sites you'll see it is quite common to get a series of 3xx responses to move from a friendly URL name to a specific resource, or to bounce around on corporate servers to the final resource.

If you're looking to do it client side in JavaScript, probably the best is to replace the window's location:  window.location.replace("..."); to cause a redirect without a back-button trail, or window.location.href("..."); to leave a back-button history.  Wrap it up in a timer if you want a short delay: window.setTimeout(function(){ window.location.replace("...") },3000); You could do something with JQuery if you want to.  However, since this is all client side you have no control. Browsers with scripts disabled, web spiders, embedded browsers, and other systems may not redirect the way you expect.

 

Each approach has different merits.  Handling the entire domain is probably best at the DNS level.  If you want to make sure everything moves the HTTP 3xx redirect responses are best done by the server.  Relying on the client to do it with JavaScript will be the least reliable approach.

This topic is closed to new replies.

Advertisement