【问题标题】:Redirect from an HTML page从 HTML 页面重定向
【发布时间】:2011-07-21 15:47:32
【问题描述】:

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

【问题讨论】:

  • 使用 .htaccess 或等效的 IIS 来执行服务器端重定向。这样,即使您的物理页面消失了,重定向仍然有效。
  • 那个 insider.zone 工具使我的重定向全部小写,导致 404。另外,没有办法联系制作该页面的人。

标签: html redirect xhtml meta html-head


【解决方案1】:

尝试使用:

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

注意:放在头部。

如果您添加快速链接以防无法正确刷新,则还适用于旧版浏览器:

&lt;p&gt;&lt;a href="http://example.com/"&gt;Redirect&lt;/a&gt;&lt;/p&gt;

将显示为

Redirect

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

【讨论】:

  • 万维网联盟 (W3C) 不鼓励使用元刷新。参考:en.wikipedia.org/wiki/Meta_refresh。所以建议改用服务器重定向。 JavaScript 重定向可能不适用于所有手机,因为 JavaScript 可能被禁用。
  • 仅供参考,0 表示 0 秒后刷新。您可能希望给用户更多时间来了解正在发生的事情。
  • @Paul Draper,这取决于您正在运行的服务器
  • 我添加了标签闭包以遵守 XHTML5 规范。
  • @NinethSense 的评论使元刷新看起来像是 JavaScript 重定向。元刷新不是 JS,并且在禁用 JS 时仍然有效。
【解决方案2】:

我会同时使用 metaJavaScript 代码,并且会有一个链接以防万一。

<!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 的配置非常简单。

【讨论】:

  • 这个重定向页面可以使用 HTML5 更简洁:pastebin.com/2qmfUZMk(适用于所有浏览器,并通过 w3c 验证)
  • @enyo 我不喜欢删除body 标签等。也许它可以验证,也许可以在常见的浏览器中使用。我确信有一些边缘情况会导致问题,而且好处似乎很小。
  • @Fricker 因为他们可能没有点击。他们可能通过语音、点击或以某种未知方式导航进行控制。它是遵循控件标准的可访问性指南,例如使用标准 HTML A 属性进行链接,并将其视为链接,并将其作为链接进行通信,而不是规定某人必须如何与之交互。此外,不建议在链接上使用click here,因为屏幕阅读器将更难导航。链接应该在描述目的地的文本上,以便用户在跟随它之前知道他们将到达哪里,以及他们如何与之交互。
  • @BillyMoon,实际上“点击”的含义已经被重载以包含所有这些含义。 “点击”就可以了。
  • @BillyMoon 在什么情况下浏览器会忽略 0 值元刷新?谢谢!
【解决方案3】:

JavaScript

<script type="text/javascript">
    window.location.href = "http://example.com";
</script>

元标记

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

【讨论】:

    【解决方案4】:

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

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

    【讨论】:

    【解决方案5】:

    这是对之前所有答案的总结,以及通过 .htaccess 使用 HTTP Refresh Header 的附加解决方案

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

    使用&lt;noscript&gt;:

    <noscript>
        <meta http-equiv="refresh" content="3" />
    </noscript>
    

    可选

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

    如果你没有被自动重定向:&lt;a href='http://example.com/alternat_url.html'&gt;Click here&lt;/a&gt;

    资源

    【讨论】:

    • 您的“Via jQuery”示例没有使用任何 jQuery。
    • 不要使用字符串调用setTimeout。而是传递一个函数。
    • "HTTP Refresh Header via .htaccess" - 为什么在询问从 HTML 页面重定向的问题时相关?
    • 如果您可以从 HTTP 标头进行重定向(例如在 .htaccess 中),那么您应该使用 301 或 302 重定向,而不是刷新。
    【解决方案6】:

    如果您希望遵循现代网络标准,则应避免使用纯 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 -->
    

    【讨论】:

    • w3.org/TR/WCAG10-HTML-TECHS/#meta-element 详细说明了为什么 W3C 不推荐使用 &lt;meta http-equiv="refresh"
    • w3c 针对 HTML 重定向提供的参数也适用于 Javascript 重定向。此外,与 HTML 重定向相比,Javascript 重定向需要启用 javascript。因此,在两者都可以使用的情况下,HTML 重定向绝对优于 Javascript 重定向。
    【解决方案7】:

    以下元标记,放置在头部之间,将告诉浏览器重定向:

    <meta http-equiv="Refresh" content="seconds; url=URL"> 
    

    将 seconds 替换为重定向前等待的秒数,并将 URL 替换为您希望重定向到的 URL。

    或者,您可以使用 JavaScript 进行重定向。将此放置在页面上任何位置的脚本标记内:

    window.location = "URL"
    

    【讨论】:

      【解决方案8】:

      最好设置一个301 redirect。请参阅 Google 的网站管理员工具文章 301 redirects

      【讨论】:

      • 这个问题专门要求一个基本的 .html 页面。我猜提问者不能修改 .htaccess 或类似的(例如使用 Github Pages 或类似的有限托管)。在这种情况下 301 是不可行的
      【解决方案9】:

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

      【讨论】:

      • mod_rewrite(仅)适用于 Apache。
      • 关于 Apache:为什么是 mod_rewrite 而不是没有额外模块的简单重定向指令?
      【解决方案10】:

      页面一加载,init 函数就会被触发并重定向页面:

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

      【讨论】:

        【解决方案11】:

        将以下代码放在 HTML 代码的

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

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

        【讨论】:

          【解决方案12】:

          将以下代码放入&lt;head&gt; 部分:

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

          【讨论】:

            【解决方案13】:

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

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

            【讨论】:

              【解决方案14】:

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

              【讨论】:

                【解决方案15】:

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

                对于 PHP:

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

                【讨论】:

                • 这不是来自 HTML 页面的重定向。
                【解决方案16】:

                我使用脚本将用户从 index.html 重定向到登录页面的 relative 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>
                

                【讨论】:

                • 上述方法中的重定向url可以是相对的。例如:您想通过网站根目录中的 index.html 重定向到登录页面或任何相关页面。不需要知道你的域名。但是当你设置 window.location.href="xxx";你必须知道域名。
                【解决方案17】:

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

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

                【讨论】:

                  【解决方案18】:

                  只需使用body标签的onload事件即可:

                  <body onload="window.location = 'http://example.com/'">
                  

                  【讨论】:

                  • 请注意,这仅在启用 javascript 时有效,如果您需要支持没有 javascript 的客户端,请改用 meta 标签方法
                  【解决方案19】:

                  Razor engine 延迟 5 秒:

                  <meta http-equiv="Refresh"
                           content="5; url=@Url.Action("Search", "Home", new { id = @Model.UniqueKey }))">
                  

                  【讨论】:

                    【解决方案20】:
                    <!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】:

                      只是为了衡量:

                      <?php
                      header("Location: http://example.com", true, 302);
                      exit;
                      ?>
                      

                      确保脚本上方没有回声,否则将被忽略。 http://php.net/manual/en/function.header.php

                      【讨论】:

                      • 这个问题专门要求一个基本的 .html 页面。我猜提问者不能修改 .htaccess 或类似的(例如使用 Github Pages 或类似的有限托管)。 PHP 在这种情况下不可用。
                      • 我认为由 PHP(预超文本处理器)生成的东西是“基本的 HTML 页面”。问题中没有任何地方提到文件类型。
                      • Eeek。为什么是状态 303?我以前从未见过。你为什么要重定向到一个电子邮件地址(一个无效的 URL)?
                      • @SimonEast 不知道,我不记得把这些值放进去,即使是 8 年前!反正修好了。
                      【解决方案22】:

                      据我了解,到目前为止我看到的所有关于这个问题的方法似乎都将旧位置添加到历史记录中。要重定向页面,但历史记录中没有旧位置,我使用replace 方法:

                      <script>
                          window.location.replace("http://example.com");
                      </script>
                      

                      【讨论】:

                        【解决方案23】:

                        您不需要任何 JavaScript 代码。在 HTML 页面的&lt;head&gt; 部分写下:

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

                        页面在 0 秒加载后,您就可以转到您的页面。

                        【讨论】:

                        • 这已包含在已接受的答案和顶级 cmets 中 - 考虑编辑答案而不是发布重复的答案
                        【解决方案24】:

                        这是一个重定向解决方案,包含我想要的一切,但无法在干净的 sn-p 中找到剪切和粘贴。

                        这个sn-p有很多优点:

                        • 让您捕获并保留人们在其 url 上拥有的任何查询字符串参数
                        • 使链接不规则以避免不必要的缓存
                        • 让您通知用户新旧站点名称
                        • 显示可设置的倒计时
                        • 可用于作为保留参数的深层链接重定向

                        使用方法:

                        如果您迁移了整个站点,则在旧服务器上停止原始站点并使用此文件创建另一个站点作为根文件夹中的默认 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>&nbsp;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>

                        【讨论】:

                          【解决方案25】:

                          你可以用javascript来做:

                          location = "url";
                          

                          【讨论】:

                            猜你喜欢
                            • 2016-03-06
                            • 2017-01-03
                            • 2012-10-15
                            • 2018-03-17
                            • 2011-10-05
                            • 1970-01-01
                            • 1970-01-01
                            • 2011-10-02
                            • 1970-01-01
                            相关资源
                            最近更新 更多