Вопрос: Перенаправление с HTML-страницы


Можно ли настроить базовую HTML-страницу для перенаправления на другую страницу при загрузке?


1331


источник


Ответы:


Попробуйте использовать:

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

Примечание. Поместите его в головную часть.

Кроме того, для старых браузеров, если вы добавите быструю ссылку, если она не обновится правильно:

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

Появится как

переадресовывать

Это все равно позволит вам добраться до того места, где вы собираетесь с дополнительным щелчком.


1907



Я бы использовал оба мета , а также Код 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 код состояния. Это легко сделать через .htaccessфайлы, использующие апаш , или через многочисленные плагины, используя WordPress , Я уверен, что есть также плагины для всех основных систем управления контентом. Кроме того, cPanel имеет очень легкую конфигурацию для 301 переадресации, если у вас это установлено на вашем сервере.


995



JavaScript

<script language="javascript">
    window.location.href = "http://example.com"
</script>

Метатег

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

107



я бы также добавьте каноническую ссылку, чтобы помочь SEO люди:

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

40



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

1. HTTP Refresh Header

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

Header set Refresh "3"

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

header("refresh: 3;");

Note that this solution is not supported by every browser.

2. JavaScript/jQuery

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:

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

3. Meta Refresh

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

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>
    <meta http-equiv="refresh" content="3" />
</noscript>

Optionally

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

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

Resources


28



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


26



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.

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

window.location = "URL"

25



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 redirect instead.

To support JavaScript-disabled browsers add a HTML meta redirect line to a noscript element. The noscript nested meta redirect combined with the canonical tag will help your search engine rankings as well.

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

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

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

19



You could use a META "redirect":

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

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

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

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


17



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

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

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

13



Put the following code in the <head> section:

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

9



You can auto redirect by HTTP Status Code 301 or 302.

For PHP:

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

9