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


当前回答

在HTML代码的<HEAD>和</HEAD>标记之间放置以下代码:

<meta HTTP-EQUIV="REFRESH" content="0; url=http://example.com/index.html">

上述HTML重定向代码将立即将访问者重定向到另一个网页。content=“0;可以更改为您希望浏览器在重定向之前等待的秒数。

其他回答

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

最好设置301重定向。请参阅谷歌网站管理员工具文章301重定向。

位于头部内部的以下元标记将告诉浏览器重定向:

<meta http-equiv="Refresh" content="seconds; url=URL"> 

将秒替换为重定向前等待的秒数,并将URL替换为要重定向到的URL。

或者,您可以使用JavaScript重定向。将其放置在页面上的任意位置的脚本标记中:

window.location = "URL"

适用于所有类型页面的简单方法是在头部添加一个元标记:

<html>
    <head>
        ...
        <meta HTTP-EQUIV="REFRESH" content="seconds; url=your.full.url/path/filename">
        ...
    </head>
    <body>
        Don't put much content, just some text and an anchor.
        Actually, you will be redirected in N seconds (as specified in content attribute).
        That's all.
        ...
    </body>
</html>

这是前面所有答案的总结,加上通过.htaccess使用HTTP Refresh Header的附加解决方案

HTTP刷新标头首先,您可以使用.htaccess设置这样的刷新标头标题集刷新“3”这相当于在PHP中使用header()函数标头(“刷新:3;”);请注意,并非每个浏览器都支持此解决方案。JavaScript使用备用URL:<脚本>setTimeout(function){location.href=“http://example.com/alternate_url.html"} , 3000);</script>没有备用URL:<脚本>setTimeout(“location.reload(true);”,timeoutPeriod);</script>使用窗口对象:<脚本>window.location.reload(true);</script>刷新标识当不需要依赖JavaScript和重定向头时,可以使用元刷新使用备用URL:<meta http equiv=“刷新”content=“3;url=http://example.com/alternate_url.html">没有备用URL:<meta http equiv=“刷新”content=“3”>使用<noscript>:<noscript><meta http equiv=“refresh”content=“3”/></noscript>

可选

根据Billy Moon的建议,如果出现问题,您可以提供刷新链接:

如果未自动重定向:<a href='http://example.com/alternat_url.html'>单击此处</a>

资源

维基百科元刷新META REFRESH的性能影响使用jQuery刷新(重新加载)一次页面?