ロード時に別のページにリダイレクトするように基本的なHTMLページを設定することは可能ですか?
25 に答える
使用してみてください:
<meta http-equiv="refresh" content="0; url=http://example.com/" />
注:ヘッドセクションに配置します。
さらに、古いブラウザの場合、正しく更新されない場合に備えてクイックリンクを追加すると、次のようになります。
<p><a href="http://example.com/">Redirect</a></p>
として表示されます
これにより、追加のクリックで目的の場所にたどり着くことができます。
メタコードと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ステータスコードを送信します。これは、 Apache.htaccessを使用してファイルを介して、またはWordPressを使用して多数のプラグインを介して簡単に実行できます。すべての主要なコンテンツ管理システム用のプラグインもあると確信しています。また、サーバーに301リダイレクトをインストールしている場合、cPanelでは301リダイレクトを非常に簡単に構成できます。
JavaScript
<script type="text/javascript">
window.location.href = "http://example.com";
</script>
メタタグ
<meta http-equiv="refresh" content="0;url=http://example.com">
また、 SEOの人々を支援するための正規リンクを追加します。
<link rel="canonical" href="http://www.example.com/product.php?item=swedish-fish"/>
これは、以前のすべての回答と、.htaccessを介したHTTP更新ヘッダーを使用した追加のソリューションの合計です。
1.HTTP更新ヘッダー
まず、.htaccessを使用して、次のような更新ヘッダーを設定できます
Header set Refresh "3"
これは、 PHPheader()で関数を使用するのと同等の「静的」です。
header("refresh: 3;");
このソリューションは、すべてのブラウザでサポートされているわけではないことに注意してください。
2. JavaScript
代替URLを使用:
<script>
setTimeout(function(){location.href="http://example.com/alternate_url.html"} , 3000);
</script>
代替URLなし:
<script>
setTimeout("location.reload(true);",timeoutPeriod);
</script>
jQuery経由:
<script>
window.location.reload(true);
</script>
3.メタリフレッシュ
JavaScriptとリダイレクトヘッダーへの依存が不要な場合は、メタリフレッシュを使用できます
代替URLの場合:
<meta http-equiv="Refresh" content="3; url=http://example.com/alternate_url.html">
代替URLなし:
<meta http-equiv="Refresh" content="3">
使用<noscript>:
<noscript>
<meta http-equiv="refresh" content="3" />
</noscript>
オプションで
Billy Moonが推奨するように、問題が発生した場合に備えて更新リンクを提供できます。
自動的にリダイレクトされない場合:<a href='http://example.com/alternat_url.html'>Click here</a>
資力
最新のWeb標準に従うことを楽しみにしている場合は、プレーンなHTMLメタリダイレクトを避ける必要があります。サーバー側のコードを作成できない場合は、代わりにJavaScriptリダイレクトを選択する必要があります。
JavaScriptが無効になっているブラウザをサポートするには、HTMLメタリダイレクト行をnoscript要素に追加します。noscriptタグと組み合わせたネストされたメタリダイレクトは、canonical検索エンジンのランキングにも役立ちます。
リダイレクトループを回避したい場合は、location.replace()JavaScript関数を使用する必要があります。
適切なクライアント側の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 -->
頭の内側の間に配置された次のメタタグは、ブラウザにリダイレクトするように指示します。
<meta http-equiv="Refresh" content="seconds; url=URL">
secondsをリダイレクトするまで待機する秒数に置き換え、URLをリダイレクト先のURLに置き換えます。
または、JavaScriptでリダイレクトすることもできます。これをスクリプトタグ内のページの任意の場所に配置します。
window.location = "URL"
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を使用することをお勧めします。
ページが読み込まれるとすぐに、init関数が実行され、ページがリダイレクトされます。
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script>
function init()
{
window.location.href = "www.wherever.com";
}
</script>
</head>
<body onload="init()">
</body>
</html>
HTMLコードの<HEAD>タグと</HEAD>タグの間に次のコードを配置します。
<meta HTTP-EQUIV="REFRESH" content="0; url=http://example.com/index.html">
上記のHTMLリダイレクトコードは、訪問者を別のWebページに即座にリダイレクトします。はcontent="0;、リダイレクトする前にブラウザが待機する秒数に変更される場合があります。
<head>セクションに次のコードを入力します。
<meta http-equiv="refresh" content="0; url=http://address/">
jQuery Mobileアプリケーションでの作業中に問題が見つかりました。場合によっては、メタヘッダータグがリダイレクトを適切に実行しないことがあります(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は、ヘッドにタグを追加することです。
<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>
ユーザーを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>
HTTPステータスコード301または302で自動リダイレクトできます。
PHPの場合:
<?php
Header("HTTP/1.1 301 Moved Permanently");
Header("Location: http://www.redirect-url.com");
?>
JavaScriptを使用する必要があります。次のコードをヘッドタグに配置します。
<script type="text/javascript">
window.location.assign("http://www.example.com")
</script>
bodyタグのonloadイベントを使用するだけです。
<body onload="window.location = 'http://example.com/'">
ちょうど良い測定のために:
<?php
header("Location: http://example.com", true, 302);
exit;
?>
スクリプトの上にエコーがないことを確認してください。そうでない場合、エコーは無視されます。 http://php.net/manual/en/function.header.php
5秒の遅延のかみそりエンジン:
<meta http-equiv="Refresh"
content="5; url=@Url.Action("Search", "Home", new { id = @Model.UniqueKey }))">
<!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>
私がそれらを理解している限り、この質問のためにこれまでに見たすべての方法は、古い場所を歴史に追加しているようです。ページをリダイレクトするが、履歴に古い場所がない場合は、次のreplace方法を使用します。
<script>
window.location.replace("http://example.com");
</script>
このためのJavaScriptコードは必要ありません。<head>HTMLページのセクションにこれを書いてください:
<meta http-equiv="refresh" content="0; url=example.com" />
ページが0秒で読み込まれるとすぐに、ページに移動できます。
これは私が欲しかったものすべてを備えたリダイレクトソリューションですが、カットアンドペーストするためのきれいなスニペットでは見つかりませんでした。
このスニペットにはいくつかの利点があります。
- 人々が彼らのURLに持っているクエリ文字列パラメータをキャッチして保持することができます
- 不要なキャッシュを回避するためにリンクをunqiueにします
- 新旧のサイト名をユーザーに通知できます
- 設定可能なカウントダウンを示しています
- パラメータを保持するため、ディープリンクリダイレクトに使用できます
使い方:
サイト全体を移行した場合は、古いサーバーで元のサイトを停止し、このファイルをルートフォルダーのデフォルトのindex.htmlファイルとして別のサイトを作成します。サイト設定を編集して、404エラーがこのindex.htmlページにリダイレクトされるようにします。これにより、サブレベルページなどへのリンクを使用して古いサイトにアクセスするすべての人を捕らえることができます。
次に、開始スクリプトタグに移動し、oldsiteおよびnewSite Webアドレスを編集し、必要に応じて秒の値を変更します。
Webサイトを保存して開始します。仕事は終わった-コーヒーの時間。
<!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> 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>
あなたはjavascriptでそれを行うことができます:
location = "url";