Jacob Tomlinson's profile picture Jacob Tomlinson
Home Blog Talks Newsletter About

Simple HTML Redirect

1 minute read #html, #web-development, #redirect, #code-snippet

I often find myself in need of a quick html redirect page. Most of the time I use the example from Stack Overflow but it involves changing the url in 3 places.

I’ve decided to create my own example with a little PHP in it to make it simpler to use.

<?php
// Change this url
$url = "http://www.example.com";
?>
<!DOCTYPE HTML>
<html lang="en-US">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="refresh" content="1;url=<?php echo $url ?>">
        <script type="text/javascript">
            window.location.href = "<?php echo $url ?>"
        </script>
        <title>Page Redirection</title>
    </head>
    <body>
        <!-- Note: don't tell people to `click` the link, just tell them that it is a link. -->
        If you are not redirected automatically, follow the <a href='<?php echo $url ?>'>link</a>.
    </body>
</html>

or if you don’t have access to PHP then just use the original one.

<!DOCTYPE HTML>
<html lang="en-US">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="refresh" content="1;url=http://example.com">
        <script type="text/javascript">
            window.location.href = "http://example.com"
        </script>
        <title>Page Redirection</title>
    </head>
    <body>
        <!-- Note: don't tell people to `click` the link, just tell them that it is a link. -->
        If you are not redirected automatically, follow the <a href='http://example.com'>link</a>.
    </body>
</html>

Have thoughts?

I love hearing feedback on my posts. You should head over to Twitter and let me know what you think!

Spotted a mistake? Why not suggest an edit!