是否可以设置基本HTML页面以在加载时重定向到另一个页面?
答案 0 :(得分:2059)
尝试使用:
<meta http-equiv="refresh" content="0; url=http://example.com/" />
注意:将其放在头部。
此外,对于旧浏览器,如果您添加快速链接,以防它无法正确刷新:
<p><a href="http://example.com/">Redirect</a></p>
将显示为
这仍然允许您通过额外点击到达目的地。
答案 1 :(得分:1064)
我会同时使用 meta 和 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可以非常轻松地配置。
答案 2 :(得分:117)
<script type="text/javascript">
window.location.href = "http://example.com";
</script>
<meta http-equiv="refresh" content="0;url=http://example.com">
答案 3 :(得分:39)
我还添加规范链接以帮助您的SEO人:
<link rel="canonical" href="http://www.example.com/product.php?item=swedish-fish"/>
答案 4 :(得分:29)
这是以前每个答案的总结加上使用HTTP刷新标题通过.htaccess的其他解决方案
<强> 1。 HTTP刷新标题
首先,您可以使用.htaccess设置像这样的刷新标题
Header set Refresh "3"
这是在 PHP
中使用header()
函数的“静态”等价物
header("refresh: 3;");
请注意,每个浏览器都不支持此解决方案。
<强> 2。的JavaScript 强>
使用候补URL:
<script>
setTimeout(function(){location.href="http://example.com/alternate_url.html"} , 3000);
</script>
没有替代网址:
<script>
setTimeout("location.reload(true);",timeoutPeriod);
</script>
通过jQuery:
<script>
window.location.reload(true);
</script>
第3。元刷新
当JavaScript和重定向标题的依赖性不受欢迎时,您可以使用元刷新
使用备用网址:
<meta http-equiv="Refresh" content="3; url=http://example.com/alternate_url.html">
没有替代网址:
<meta http-equiv="Refresh" content="3">
使用<noscript>
:
<noscript>
<meta http-equiv="refresh" content="3" />
</noscript>
<强>任选地强>
根据Billy Moon的建议,如果出现问题,您可以提供刷新链接:
如果您未自动重定向:<a href='http://example.com/alternat_url.html'>Click here</a>
<强>资源强>
答案 5 :(得分:27)
以下元标记位于头部内部,将告诉浏览器重定向:
<meta http-equiv="Refresh" content="seconds; url=URL">
将秒替换为重定向前等待的秒数,并将URL替换为您希望其重定向到的URL。
或者,您可以使用JavaScript重定向。将其放在页面上任何位置的脚本标记内:
window.location = "URL"
答案 6 :(得分:26)
最好设置一个301 redirect。请参阅Google的网站管理员工具文章 301 redirects 。
答案 7 :(得分:25)
如果您期待遵循现代Web标准,则应避免使用纯HTML元重定向。如果您无法创建服务器端代码,则应选择 JavaScript重定向。
要支持禁用JavaScript的浏览器,请将HTML元重定向行添加到noscript
元素。 noscript
嵌套元重定向与canonical
标记相结合,也有助于您的搜索引擎排名。
如果您想避免重定向循环,则应使用location.replace()
JavaScript函数。
正确的客户端URL redirect代码如下所示(使用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 -->
答案 8 :(得分:17)
您可以使用 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 。
答案 9 :(得分:14)
一旦页面加载,就会触发init
函数并重定向页面:
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script>
function init()
{
window.location.href = "www.wherever.com";
}
</script>
</head>
<body onload="init()">
</body>
</html>
答案 10 :(得分:10)
将以下代码放在&lt; HEAD&gt;之间。和&lt; / HEAD&gt; HTML代码的标签:
<meta HTTP-EQUIV="REFRESH" content="0; url=http://example.com/index.html">
上述HTML重定向代码会立即将访问者重定向到其他网页。 content="0;
可能会更改为您希望浏览器在重定向之前等待的秒数。
答案 11 :(得分:9)
将以下代码放入<head>
部分:
<meta http-equiv="refresh" content="0; url=http://address/">
答案 12 :(得分:9)
我在使用jQuery Mobile应用程序时遇到了问题,在某些情况下,我的Meta标头标签无法正确实现重定向(jQuery Mobile不会自动为每个页面读取标头,因此把JavaScript放在那里也是无效的,除非把它包装在复杂性中)。我发现在这种情况下最简单的解决方案是将JavaScript重定向直接放入文档正文中,如下所示:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="refresh" content="0;url=myURL" />
</head>
<body>
<p>You are not logged in!</p>
<script language="javascript">
window.location = "myURL";
</script>
</body>
</html>
这似乎适用于我。
答案 13 :(得分:8)
适用于所有类型网页的简单方法只是在头部添加meta
标记:
<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>
答案 14 :(得分:7)
您可以按HTTP状态码301或302自动重定向。
对于PHP:
<?php
Header("HTTP/1.1 301 Moved Permanently");
Header("Location: http://www.redirect-url.com");
?>
答案 15 :(得分:7)
您应该使用JavaScript。将以下代码放在头标记中:
<script type="text/javascript">
window.location.assign("http://www.example.com")
</script>
答案 16 :(得分:6)
只需使用body标签的onload事件:
function bullet(owner){
//Your Bullet properties
this.owner = owner;
}
答案 17 :(得分:6)
我使用的脚本将用户从index.html重定向到Login Page
<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>
答案 18 :(得分:6)
只是好好衡量:
<?php
header("Location: example@example.com", TRUE, 303);
exit;
?>
确保脚本上方没有回声,否则将被忽略。 http://php.net/manual/en/function.header.php
答案 19 :(得分:5)
Razor engine延迟5秒:
<meta http-equiv="Refresh"
content="5; url=@Url.Action("Search", "Home", new { id = @Model.UniqueKey }))">
答案 20 :(得分:5)
<!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>
答案 21 :(得分:4)
您不需要任何JavaScript代码。将其写在HTML页面的<head>
部分:
<meta http-equiv="refresh" content="0; url=example.com" />
只要页面加载0秒,您就可以转到您的页面。
答案 22 :(得分:3)
据我所知,到目前为止我在这个问题上看到的所有方法似乎都将旧位置添加到历史记录中。要重定向页面,但历史记录中没有旧位置,我使用replace
方法:
<script>
window.location.replace("http://example.com");
</script>
答案 23 :(得分:2)
这是一个重定向解决方案,包含我想要的所有内容,但却找不到一个干净整洁的片段来剪切&amp;糊。
此代码段有许多优点:
使用方法:
如果迁移了整个站点,则在旧服务器上停止原始站点,并使用此文件创建另一个站点作为根文件夹中的默认index.html文件。编辑站点设置,以便将任何404错误重定向到此index.html页面。这会抓住访问旧网站的任何人,并链接到子级别页面等。
现在转到开始脚本标记并编辑oldsite和newSite网址,并根据需要更改秒值。
保存并启动您的网站。完成工作 - 喝咖啡的时间。
<!DOCTYPE html>
<html>
<head>
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
<META HTTP-EQUIV="EXPIRES" CONTENT="Mon, 22 Jul 2002 11:12:01 GMT">
<style>
body { margin: 200px; font: 12pt helvetica; }
</style>
</head>
<body>
</body>
<script type="text/javascript">
// Edit these to suit your needs.
var oldsite = 'http://theoldsitename.com'
var newSite = "https://thenewsitename.com";
var seconds = 20; // countdown delay.
var path = location.pathname;
var srch = location.search;
var uniq = Math.floor((Math.random() * 10000) + 1);
var newPath = newSite + path + (srch === '' ? "?" + uniq : srch + "&" + uniq);
document.write ('<p>As part of hosting improvements, the system has been migrated from ' + oldsite + ' to</p>');
document.write ('<p><a href="' + newPath + '">' + newSite + '</a></p>');
document.write ('<p>Please take note of the new website address.</p>');
document.write ('<p>If you are not automatically redirected please click the link above to proceed.</p>');
document.write ('<p id="dvCountDown">You will be redirected after <span id = "lblCount"></span> seconds.</p>');
function DelayRedirect() {
var dvCountDown = document.getElementById("dvCountDown");
var lblCount = document.getElementById("lblCount");
dvCountDown.style.display = "block";
lblCount.innerHTML = seconds;
setInterval(function () {
seconds--;
lblCount.innerHTML = seconds;
if (seconds == 0) {
dvCountDown.style.display = "none";
window.location = newPath;
}
}, 1000);
}
DelayRedirect()
</script>
</html>
&#13;
答案 24 :(得分:2)
使用JavaScript进行重定向,如果禁用了JS,则<noscript>
。
<script>
window.location.replace("https://google.com");
</script>
<noscript>
<a href="https://google.com">Click here if you are not redirected automatically.</a>
</noscript>
答案 25 :(得分:0)
使用此代码:
<meta http-equiv="refresh" content="0; url=https://google.com/" />
请注意: 将其放入头标。
答案 26 :(得分:0)
您可以使用javascript完成此操作:
location = "url";