Redirect from an HTML page

2018-12-31 02:58发布

Is it possible to set up a basic HTML page to redirect to another page on load?

25条回答
时光乱了年华
2楼-- · 2018-12-31 03:29

As soon as the page loads, the init function is fired and the page is redirected:

<!DOCTYPE html>
<html>
    <head>
        <title>Example</title>
        <script>
            function init()
            {
               window.location.href = "www.wherever.com";
            }
        </script>
    </head>

    <body onload="init()">
    </body>
</html>
查看更多
无色无味的生活
3楼-- · 2018-12-31 03:29

As far as I understand them, all the methods I have seen so far for this question seem to add the old location to the history. To redirect the page, but do not have the old location in the history, I use the replace method:

<script>
    window.location.replace("http://example.com");
</script>
查看更多
宁负流年不负卿
4楼-- · 2018-12-31 03:30

You should use JavaScript. Place the following code in your head tags:

<script type="text/javascript">
 window.location.assign("http://www.example.com")
</script>
查看更多
时光乱了年华
5楼-- · 2018-12-31 03:31

It would be better to set up a 301 redirect. See the Google's Webmaster Tools article 301 redirects.

查看更多
爱死公子算了
6楼-- · 2018-12-31 03:31

Razor engine for a 5 second delay:

<meta http-equiv="Refresh"
         content="5; url=@Url.Action("Search", "Home", new { id = @Model.UniqueKey }))">
查看更多
心情的温度
7楼-- · 2018-12-31 03:31

This is a redirect solution with everything I wanted but could not find in a nice clean snippet to cut & paste.

This snippet has a number of advantages:

  • lets you catch and retain any querystring params folks have on their url
  • makes the link unqiue to avoid unwanted caching
  • lets you inform users of the old and new site names
  • shows a settable countdown
  • can be used for deep-link redirects as retains params

How to use:

If you migrated an entire site then on the old server stop the original site and create another with this file as the default index.html file in the root folder. Edit the site settings so that any 404 error is redirected to this index.html page. This catches anyone who accesses the old site with a link into a sub-level page etc.

Now go to the opening script tag and edit the oldsite and newSite web addresses, and change the seconds value as needed.

Save and start your website. Job done - time for a coffee.

<!DOCTYPE html>
<html>
<head>
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
<META HTTP-EQUIV="EXPIRES" CONTENT="Mon, 22 Jul 2002 11:12:01 GMT">
<style>
body { margin: 200px; font: 12pt helvetica; }
</style>

</head>
<body>

</body>
<script type="text/javascript">

// Edit these to suit your needs.
var oldsite = 'http://theoldsitename.com'
var newSite = "https://thenewsitename.com";
var seconds = 20;  // countdown delay.

var path = location.pathname;
var srch = location.search;
var uniq = Math.floor((Math.random() * 10000) + 1);
var newPath = newSite + path + (srch === '' ? "?" + uniq : srch + "&" + uniq); 


document.write ('<p>As part of hosting improvements, the system has been migrated from ' + oldsite + ' to</p>');
document.write ('<p><a href="' + newPath + '">' + newSite + '</a></p>');
document.write ('<p>Please take note of the new website address.</p>');
document.write ('<p>If you are not automatically redirected please click the link above to proceed.</p>');
document.write ('<p id="dvCountDown">You will be redirected after <span id = "lblCount"></span>&nbsp;seconds.</p>');

function DelayRedirect() {
    var dvCountDown = document.getElementById("dvCountDown");
    var lblCount = document.getElementById("lblCount");
    dvCountDown.style.display = "block";
    lblCount.innerHTML = seconds;
    setInterval(function () {
        seconds--;
        lblCount.innerHTML = seconds;
        if (seconds == 0) {
            dvCountDown.style.display = "none";
            window.location = newPath;
        }
    }, 1000);
}
DelayRedirect()

</script>
</html>

查看更多
登录 后发表回答