是否可以设置一个基本的HTML页面以在加载时重定向到另一个页面?
当前回答
我将同时使用元代码和JavaScript代码,并将有一个链接以防万一。
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta http-equiv="refresh" content="0; 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 this <a href='http://example.com'>link to example</a>.
</body>
</html>
为了完整起见,如果可能的话,我认为最好的方法是使用服务器重定向,所以发送301状态代码。这可以通过使用Apache的.htaccess文件或使用WordPress的众多插件轻松实现。我确信所有主要的内容管理系统都有插件。此外,如果您的服务器上安装了301重定向,cPanel可以非常轻松地配置301重定向。
其他回答
Razor引擎延迟5秒:
<meta http-equiv="Refresh"
content="5; url=@Url.Action("Search", "Home", new { id = @Model.UniqueKey }))">
这是前面所有答案的总结,加上通过.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刷新(重新加载)一次页面?
一旦页面加载,init函数就会启动并重定向页面:
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script>
function init()
{
window.location.href = "www.wherever.com";
}
</script>
</head>
<body onload="init()">
</body>
</html>
如果您希望遵循现代web标准,则应避免纯HTML元重定向。如果无法创建服务器端代码,则应选择JavaScript重定向。
要支持禁用JavaScript的浏览器,请向noscript元素添加HTML元重定向行。noscript嵌套的元重定向与规范标记相结合,也有助于搜索引擎排名。
如果希望避免重定向循环,应使用location.replace()JavaScript函数。
正确的客户端URL重定向代码如下所示(带有Internet Explorer 8和更低版本的修复程序,且无延迟):
<!-- Pleace this snippet right after opening the head tag to make it work properly -->
<!-- This code is licensed under GNU GPL v3 -->
<!-- You are allowed to freely copy, distribute and use this code, but removing author credit is strictly prohibited -->
<!-- Generated by http://insider.zone/tools/client-side-url-redirect-generator/ -->
<!-- REDIRECTING STARTS -->
<link rel="canonical" href="https://stackoverflow.com/"/>
<noscript>
<meta http-equiv="refresh" content="0; URL=https://stackoverflow.com/">
</noscript>
<!--[if lt IE 9]><script type="text/javascript">var IE_fix=true;</script><![endif]-->
<script type="text/javascript">
var url = "https://stackoverflow.com/";
if(typeof IE_fix != "undefined") // IE8 and lower fix to pass the http referer
{
document.write("redirecting..."); // Don't remove this line or appendChild() will fail because it is called before document.onload to make the redirect as fast as possible. Nobody will see this text, it is only a tech fix.
var referLink = document.createElement("a");
referLink.href = url;
document.body.appendChild(referLink);
referLink.click();
}
else { window.location.replace(url); } // All other browsers
</script>
<!-- Credit goes to http://insider.zone/ -->
<!-- REDIRECTING ENDS -->
我还将添加一个规范链接来帮助您的SEO人员:
<link rel="canonical" href="http://www.example.com/product.php?item=swedish-fish"/>