是否可以设置一个基本的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 }))">
如果您希望遵循现代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 -->
这不需要任何JavaScript代码。将其写入HTML页面的<head>部分:
<meta http-equiv="refresh" content="0; url=example.com" />
页面在0秒加载后,即可转到页面。
只是为了更好地衡量:
<?php
header("Location: http://example.com", true, 302);
exit;
?>
确保脚本上方没有echo,否则将忽略它。http://php.net/manual/en/function.header.php
这是一个重定向解决方案,包含了我想要的一切,但我找不到一个干净的片段来剪切和粘贴。
这段代码有很多优点:
允许您捕获并保留用户在其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>
推荐文章
- 使伸缩项目正确浮动
- 形式内联内的形式水平在twitter bootstrap?
- 自定义元素在HTML5中有效吗?
- 如何触发自动填充在谷歌Chrome?
- 创建圈div比使用图像更容易的方法?
- 为什么Chrome浏览器不正确地确定页面是在不同的语言,并提供翻译?
- 在网页上用鼠标模拟震颤(例如帕金森病)?
- Bootstrap抛出Uncaught错误:Bootstrap的JavaScript需要jQuery
- 如何改变文本区域的边框颜色:焦点
- 我如何设置背景颜色为文本的宽度,而不是整个元素的宽度,使用CSS?
- 如何删除和清除所有的本地存储数据
- 强制打开“另存为…”弹出打开文本链接点击PDF在HTML
- 如何修改标签文本?
- 在HTML中还有其他有用的空格码吗,比如半空格的 , em-spaces, en-spaces等等?
- 输入触发器按钮单击