是否可以设置一个基本的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刷新(重新加载)一次页面?
其他回答
您可以使用META“重定向”:
<meta http-equiv="refresh" content="0; url=http://new.example.com/address" />
或JavaScript重定向(请注意,并非所有用户都启用了JavaScript,因此始终为他们准备备份解决方案)
<script language="javascript">
window.location = "http://new.example.com/address";
</script>
但如果您有选择,我建议您使用mod_rewrite。
这是一个重定向解决方案,包含了我想要的一切,但我找不到一个干净的片段来剪切和粘贴。
这段代码有很多优点:
允许您捕获并保留用户在其URL上的任何查询字符串参数使链接唯一,以避免不必要的缓存允许您通知用户新旧站点名称显示可设置的倒计时可以作为保留参数用于深度链接重定向
如何使用:
如果您迁移了整个站点,则在旧服务器上停止原始站点,并在根文件夹中创建另一个将此文件作为默认index.html文件的站点。编辑站点设置,以便将任何404错误重定向到此index.html页面。这将捕获任何通过链接访问旧站点的人,链接到子级页面等。
现在转到打开的脚本标记,编辑oldsite和newSite网址,并根据需要更改秒值。
保存并启动您的网站。工作完成了——该喝杯咖啡了。
<!DOCTYPE html><html><head><META HTTP-EQUIV=“cachecontrol”CONTENT=“NO-CACHE”><META HTTP-EQUIV=“PRAGMA”CONTENT=“NO-CACHE”><META HTTP-EQUIV=“到期”内容=“2002年7月22日星期一11:12:01 GMT”><style>正文{margin:200px;font:12pt helvetica;}</style></head><body></body><script type=“text/javascript”>//编辑这些以满足您的需求。var oldsite='http://theoldsitename.com'var newSite=“https://thenewsitename.com";var秒=20;//倒计时延迟。var路径=位置.pathname;var srch=位置搜索;var uniq=数学地板((Math.random()*10000)+1);var newPath=新站点+路径+(srch==“”?“?”+uniq:srch+“&”+uniq);document.write(作为主机改进的一部分,系统已从'+oldsite+'迁移到</p>');document.write('<p><a href=“'+newPath+'”>'+newSite+'</a></p>');document.write(“<p>请注意新的网站地址。</p>”);document.write(“<p>如果未自动重定向,请单击上面的链接继续。</p>”);document.write('<p id=“dvCountDown”>您将在<span id=“lblCount”></span>秒后被重定向。</p>');函数DelayRedirect(){var dvCountDown=document.getElementById(“dvCountDown”);var lblCount=document.getElementById(“lblCount”);dvCountDown.style.display=“块”;lblCount.innerHTML=秒;setInterval(函数(){秒--;lblCount.innerHTML=秒;如果(秒==0){dvCountDown.style.display=“无”;window.location=newPath;}}, 1000);}延迟重定向()</script></html>
我使用一个脚本将用户从index.html重定向到登录页面的相对url
<html>
<head>
<title>index.html</title>
</head>
<body onload="document.getElementById('lnkhome').click();">
<a href="/Pages/Login.aspx" id="lnkhome">Go to Login Page</a>
</body>
</html>
只需使用body标签的onload事件:
<body onload="window.location = 'http://example.com/'">
如果您希望遵循现代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 -->