在加载页面时,是否可以设置一个基本的HTML页面以重定向到另一个页面?
尝试使用以下代码:
<meta http-equiv="refresh" content="0; url=http://example.com/" />
注意:将其放置在 <head>
部分。
另外,对于旧版浏览器,如果您添加了一个快速链接以防止刷新不正确:
<p><a href="http://example.com/">Redirect</a></p>
将显示为
这仍将允许您使用额外的单击跳转到目标页面。
我会同时使用元标签和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的众多插件完成。我相信对于所有主要内容管理系统也都有插件可用。此外,如果您在服务器上安装了cPanel,则非常容易配置301重定向。
body
标签等。虽然可能符合验证要求,在常见浏览器中也可以正常运行,但我相信在某些特殊情况下它可能会引发问题,而且好处似乎并不大。 - Billy Moon<script type="text/javascript">
window.location.href = "http://example.com";
</script>
<meta http-equiv="refresh" content="0;url=http://example.com">
我还会添加一个canonical链接来帮助你的SEO人员:
<link rel="canonical" href="http://www.example.com/product.php?item=swedish-fish"/>
这是对之前所有答案的总结,另外提供一种使用HTTP刷新头部的解决方案,通过.htaccess文件实现。
HTTP刷新头部
首先,可以使用.htaccess文件设置一个刷新头部,如下所示:
Header set Refresh "3"
这是在PHP中使用header()
函数的“静态”等效方式。
header("refresh: 3;");
请注意,这种解决方案并非所有浏览器都支持。
JavaScript
使用替代的URL:
<script>
setTimeout(function(){location.href="http://example.com/alternate_url.html"} , 3000);
</script>
没有备用网址:
<script>
setTimeout("location.reload(true);",timeoutPeriod);
</script>
使用window
对象:
<script>
window.location.reload(true);
</script>
元重定向
如果不想依赖JavaScript和重定向头,则可以使用元重定向
使用备用URL:
<meta http-equiv="Refresh" content="3; url=http://example.com/alternate_url.html">
没有备用 URL:
<meta http-equiv="Refresh" content="3">
使用<noscript>
标签:
<noscript>
<meta http-equiv="refresh" content="3" />
</noscript>
可选项
根据Billy Moon的建议,您可以提供一个刷新链接以防出现问题:
If you are not redirected automatically: <a href='http://example.com/alternat_url.html'>Click here</a>
资源
setTimeout
,应该传递一个函数作为参数。 - Oriol.htaccess
中)进行重定向,则应使用301或302重定向,而不是刷新。 - Simon East<meta http-equiv="Refresh" content="seconds; url=URL">
window.location = "URL"
noscript
元素中。noscript
嵌套的meta重定向与canonical
标记结合使用,还可帮助提高搜索引擎排名。location.replace()
JavaScript函数。<!-- 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 -->
<meta http-equiv="refresh"
的详细信息。 - daxelrod您可以使用 META “redirect”:
<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。
页面加载后,init
函数会立即触发并重定向页面:
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script>
function init()
{
window.location.href = "www.wherever.com";
}
</script>
</head>
<body onload="init()">
</body>
</html>