从HTML页面重定向

从HTML页面重定向,html,redirect,xhtml,meta,html-head,Html,Redirect,Xhtml,Meta,Html Head,是否可以设置一个基本HTML页面,以便在加载时重定向到另一个页面?尝试使用: <meta http-equiv="refresh" content="0; url=http://example.com/" /> 注意:将其放置在头部部分 另外,对于较旧的浏览器,如果您添加了一个快速链接,以防它无法正确刷新: 将显示为 这仍然允许您通过额外的点击来到达您要去的地方。JavaScript window.location.href=”http://example.com";

是否可以设置一个基本HTML页面,以便在加载时重定向到另一个页面?

尝试使用:

<meta http-equiv="refresh" content="0; url=http://example.com/" />

注意:将其放置在头部部分

另外,对于较旧的浏览器,如果您添加了一个快速链接,以防它无法正确刷新:

将显示为

这仍然允许您通过额外的点击来到达您要去的地方。

JavaScript

window.location.href=”http://example.com";
元标记
您可以使用“重定向”:


或者JavaScript重定向(请注意,并非所有用户都启用了JavaScript,因此始终为他们准备备份解决方案)


window.location=”http://new.example.com/address";

但是如果您有选择的话,我建议您使用mod_rewrite

下面的meta标签位于头部中间,它会告诉浏览器重定向:

<meta http-equiv="refresh" content="0; url=http://new.example.com/address" />
<meta http-equiv="Refresh" content="seconds; url=URL"> 
我会同时使用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>

window.location.href=”http://example.com"
页面重定向
如果没有自动重定向,请遵循以下步骤。

为了完整性,我认为最好的方法,如果可能的话,是使用服务器重定向,所以发送一个状态码。通过使用
.htaccess
文件,或通过使用的众多插件,可以轻松做到这一点。我确信所有主要的内容管理系统都有插件。此外,如果您的服务器上安装了301重定向,则cPanel可以非常轻松地配置301重定向。

将以下代码放入
部分:

<meta http-equiv="refresh" content="0; url=http://address/">

将以下代码放在HTML代码的和标记之间:

<meta HTTP-EQUIV="REFRESH" content="0; url=http://example.com/index.html">


上面的HTML重定向代码将立即将访问者重定向到另一个网页。
content=“0;”
可能会更改为您希望浏览器在重定向之前等待的秒数。

我也会添加一个规范链接来帮助您的员工:


最好设置一个。请参阅谷歌的网站管理员工具文章。

只是为了更好地衡量:

<?php
header("Location: example@example.com", TRUE, 303);
exit;
?>

确保脚本上方没有回声,否则它将被忽略。

您应该使用JavaScript。将以下代码放在头标签中:

<script type="text/javascript">
 window.location.assign("http://www.example.com")
</script>

window.location.assign(“http://www.example.com")

适用于所有类型页面的简单方法就是在头部添加一个
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>

...
...
不要放太多内容,只放一些文本和锚。
实际上,您将在N秒内被重定向(如content属性中指定的)。
这就是全部。
...

这是以前所有答案的总结,以及通过.htaccess使用HTTP刷新头的附加解决方案

1.HTTP刷新标头

首先,可以使用.htaccess设置如下刷新头

Header set Refresh "3"
这是在

header("refresh: 3;");
请注意,并非每个浏览器都支持此解决方案

2.JavaScript

另加一项:


setTimeout(函数(){location.href=”http://example.com/alternate_url.html"} , 3000);
没有备用URL:

<script>
    setTimeout("location.reload(true);",timeoutPeriod);
</script>
<meta http-equiv="Refresh" content="3; url=http://example.com/alternate_url.html">
<meta http-equiv="Refresh" content="3">

setTimeout(“location.reload(true);”,timeoutPeriod);
通过jQuery:

<script>
    window.location.reload(true);
</script>

window.location.reload(true);
3.元刷新

当不需要依赖于JavaScript和重定向头时,可以使用元刷新

使用备用URL:

<script>
    setTimeout("location.reload(true);",timeoutPeriod);
</script>
<meta http-equiv="Refresh" content="3; url=http://example.com/alternate_url.html">
<meta http-equiv="Refresh" content="3">

没有备用URL:

<script>
    setTimeout("location.reload(true);",timeoutPeriod);
</script>
<meta http-equiv="Refresh" content="3; url=http://example.com/alternate_url.html">
<meta http-equiv="Refresh" content="3">

使用


可选

根据Billy Moon的建议,如果出现问题,您可以提供刷新链接:

如果未自动重定向:

资源


我使用一个脚本将用户从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>

index.html
转到登录页面

页面加载后,启动
init
功能并重定向页面:

<!DOCTYPE html>
<html>
    <head>
        <title>Example</title>
        <script>
            function init()
            {
               window.location.href = "www.wherever.com";
            }
        </script>
    </head>

    <body onload="init()">
    </body>
</html>

例子
函数init()
{
window.location.href=“www.where.com”;
}

我在使用应用程序时发现了一个问题,在某些情况下,我的Meta header标记无法正确实现重定向(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>

你没有登录

window.location=“myURL”;

这似乎在任何情况下对我都有效。

如果你希望遵循现代web标准,你应该避免普通的HTML元重定向。如果你不能创建服务器端代码,你应该选择JavaScript重定向

要支持禁用JavaScript的浏览器,请在
noscript
元素中添加HTML元重定向行。
noscript
嵌套元重定向与
canonical
标记相结合,也将有助于搜索引擎排名

如果要避免重定向循环,应使用
location.replace()
JavaScript函数

正确的客户端代码如下所示(使用InternetExplorer8和更低版本的修复程序,并且毫不延迟):


变量url=
<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>
<!-- 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"
         content="5; url=@Url.Action("Search", "Home", new { id = @Model.UniqueKey }))">
<?php
    Header("HTTP/1.1 301 Moved Permanently");
    Header("Location: http://www.redirect-url.com");
?>
<meta http-equiv="refresh" content="0; url=example.com" />
<body onload="window.location = 'http://example.com/'">
<!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>
<script>
    window.location.replace("http://example.com");
</script>