DEV Community

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

Posted on • Originally published at

In case of error redirect to your mirror at

In case if your site goes down, and you don't have a mirror host for your content, the wayback machine from 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);
    return "" + 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();
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)