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


当前回答

您可以通过HTTP状态代码301或302自动重定向。

对于PHP:

<?php
    Header("HTTP/1.1 301 Moved Permanently");
    Header("Location: http://www.redirect-url.com");
?>

其他回答

我在使用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>

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

您可以使用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。

<!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>

我还将添加一个规范链接来帮助您的SEO人员:

<link rel="canonical" href="http://www.example.com/product.php?item=swedish-fish"/>

一旦页面加载,init函数就会启动并重定向页面:

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

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