DEV Community

Cover image for In case of error redirect to your mirror at archive.org
Samuel Viana
Samuel Viana

Posted on • Originally published at codehouse.digfish.org

In case of error redirect to your mirror at archive.org

In case if your site goes down, and you don't have a mirror host for your content, the wayback machine from archive.org provides you with such a thing you can make use of.
Just provide your http server or web framework to detect an error (in PHP you can use the functions set_error_handler and set_exception_handler) and redirect to a page that will redirect you, using Javascript, to the corresponding archived content at the WayBackMachine.


The following code does the job:

function archiveorgcacheurl() {
    origurl = location.href;
    url = new URL(origurl);
    console.log(url);
    return "https://web.archive.org/web/" + url.protocol + "//" + url.hostname;
}

Enter fullscreen mode Exit fullscreen mode

Insert the following code at the bottom of your error page html:

<script type="text/javascript">
setTimeout(function() {
    location.href = archiveorgcacheurl();
},5000);
</script>
Enter fullscreen mode Exit fullscreen mode

After five seconds, it will redirect automatically to the mirrored copy at the wayback machine.

In case you don't want your users to wait this time lapse you can provide a link or button with the following html snippet:

While the error is not solved you can visit a mirrored copy at <A href="javascript:" onclick="this.href=archiveorgcacheurl()">WaybackMachine Cache Page</A>
<h2>Redirecting you automatically... Please wait...</h2>
Enter fullscreen mode Exit fullscreen mode

The complete code for the html page is in the Gist below:

Top comments (0)