从 HTML 页面重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5411538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 07:32:38  来源:igfitidea点击:

Redirect from an HTML page

htmlredirectxhtmlmetahtml-head

提问by chobo

Is it possible to set up a basic HTML page to redirect to another page on load?

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

回答by Valerij

Try using:

尝试使用:

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

Note: Place it in the head section.

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

Additionally for older browsers if you add a quick link in case it doesn't refresh correctly:

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

<p><a href="http://example.com/">Redirect</a></p>

<p><a href="http://example.com/">Redirect</a></p>

Will appear as

将显示为

Redirect

重定向

This will still allow you to get to where you're going with an additional click.

这仍然可以让您通过额外的点击到达您要去的地方。

回答by Billy Moon

I would use both meta, and JavaScript codeand would have a link just in case.

我会同时使用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>

For completeness, I think the best way, if possible, is to use server redirects, so send a 301status code. This is easy to do via .htaccessfiles using Apache, or via numerous plugins using WordPress. I am sure there are also plugins for all the major content management systems. Also, cPanel has very easy configuration for 301 redirects if you have that installed on your server.

为了完整起见,我认为最好的方法(如果可能)是使用服务器重定向,因此发送301状态代码。这很容易通过.htaccess使用Apache 的文件或通过使用WordPress 的众多插件来完成。我相信所有主要的内容管理系统也有插件。此外,如果您的服务器上安装了 cPanel,则它可以为 301 重定向提供非常简单的配置。

回答by amit_g

JavaScript

JavaScript

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

Meta tag

元标签

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

回答by lrkwz

I would alsoadd a canonical link to help your SEOpeople:

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

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

回答by Peter Olson

The following meta tag, placed between inside the head, will tell the browser to redirect:

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

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

Replace seconds with the number of seconds to wait before it redirects, and replace URL with the URL you want it to redirect to.

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

Alternatively, you can redirect with JavaScript. Place this inside of a script tag anywhere on the page:

或者,您可以使用 JavaScript 重定向。将它放在页面上任意位置的脚本标签内:

window.location = "URL"

回答by RafaSashi

This is a sum up of every previous answers plus an additional solution using HTTP Refresh Header via .htaccess

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

1. HTTP Refresh Header

1. HTTP 刷新头

First of all, you can use .htaccess to set a refresh header like this

首先,您可以使用 .htaccess 来设置这样的刷新标头

Header set Refresh "3"

This is the "static" equivalent of using the header()function in PHP

这是header()PHP中使用该函数的“静态”等效项

header("refresh: 3;");

Note that this solution is not supported by every browser.

请注意,并非每个浏览器都支持此解决方案。

2. JavaScript

2. JavaScript

With an alternate URL:

使用备用网址

<script>
    setTimeout(function(){location.href="http://example.com/alternate_url.html"} , 3000);
</script>

Without an alternate URL:

没有备用网址:

<script>
    setTimeout("location.reload(true);",timeoutPeriod);
</script>

Via jQuery:

通过 jQuery:

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

3. Meta Refresh

3.元刷新

You can use meta refresh when dependencies on JavaScript and redirect headers are unwanted

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

With an alternate URL:

使用备用网址:

<meta http-equiv="Refresh" content="3; url=http://example.com/alternate_url.html">

Without an alternate URL:

没有备用网址:

<meta http-equiv="Refresh" content="3">

Using <noscript>:

使用<noscript>

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

Optionally

可选

As recommended by Billy Moon, you can provide a refresh link in case something goes wrong:

按照比利·穆恩 (Billy Moon) 的建议,您可以提供刷新链接,以防出现问题:

If you are not redirected automatically: <a href='http://example.com/alternat_url.html'>Click here</a>

如果您没有自动重定向: <a href='http://example.com/alternat_url.html'>Click here</a>

Resources

资源

回答by Patartics Milán

If you are looking forward to follow modern web standards, you should avoid plain HTML meta redirects. If you can not create server-side code, you should choose JavaScript redirectinstead.

如果您希望遵循现代 Web 标准,则应避免使用纯 HTML 元重定向。如果您无法创建服务器端代码,则应选择JavaScript 重定向

To support JavaScript-disabled browsers add a HTML meta redirect line to a noscriptelement. The noscriptnested meta redirect combined with the canonicaltag will help your search engine rankings as well.

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

If you would like to avoid redirect loops, you should use the location.replace()JavaScript function.

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

A proper client-side URL redirectcode looks like this (with an Internet Explorer 8 and lower fix and without delay):

正确的客户端URL 重定向代码如下所示(使用 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 -->

回答by Alex K

It would be better to set up a 301 redirect. See the Google's Webmaster Tools article 301 redirects.

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

回答by Czechnology

You could use a META"redirect":

您可以使用META“重定向”:

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

or JavaScriptredirect (note that not all users have JavaScript enabled so always prepare a backup solution for them)

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

<script language="javascript">
  window.location = "http://new.example.com/address";
</script>

But I'd rather recommend using mod_rewrite, if you have the option.

但我更愿意推荐使用mod_rewrite,如果你有选择。

回答by kkk

As soon as the page loads, the initfunction is fired and the page is redirected:

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

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

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