如何在页面加载时将一个HTML页面重定向到另一个页面

1824

在加载页面时,是否可以设置一个基本的HTML页面以重定向到另一个页面?


2
使用.htaccess或IIS等同工具进行服务器端重定向。这样,即使您的物理页面消失,重定向仍将起作用。 - flith
4
那个 insider.zone 工具把我的重定向链接都变成了小写字母,导致出现了404错误页面。另外,也没有办法联系到制作这个页面的人。 - Dan Jacobson
24个回答

5

Razor引擎进行5秒延迟:

<meta http-equiv="Refresh"
         content="5; url=@Url.Action("Search", "Home", new { id = @Model.UniqueKey }))">

5
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Redirect to a page</title>
    </head>
    <body onload="window.location.assign('http://example.com')">

    </body>
</html>

4
这是一个重定向解决方案,包含我想要的一切,但我找不到一个漂亮简洁的片段来复制和粘贴。
这个片段有许多优点:
- 可以捕获并保留 URL 上任何查询字符串参数 - 使链接唯一以避免不必要的缓存 - 让你告知用户旧站点和新站点名称 - 显示可设置的倒计时 - 可用于深度链接重定向以保留参数
使用方法:
如果您迁移了整个网站,则在旧服务器上停止原始站点,并创建另一个将此文件作为根目录中默认 index.html 文件的站点。编辑站点设置,以便将任何 404 错误重定向到此 index.html 页面。这会捕获任何通过链接进入旧站点的人,即使是子级页面等。
现在转到打开的脚本标记并编辑 oldsite 和 newSite 网址,并根据需要更改秒数值。
保存并启动您的网站。工作完成 - 该喝杯咖啡了。

<!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>


3
您不需要任何JavaScript代码来实现此功能。只需在HTML页面的部分编写以下内容即可:
<meta http-equiv="refresh" content="0; url=example.com" />

页面加载完毕后(0秒),您可以访问您的页面。

3
已经有接受的答案和热门评论涉及到了这个问题,请考虑编辑答案而不是发布重复的内容。 - RozzA

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接