質問
基本的なHTMLページを設定して、ロード中の別のページにリダイレクトすることは可能ですか?
解決
使用してみてください:
<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 ステータスコード。これは簡単に行うことができます .htaccess
使用するファイル アパッチ, 、または多数のプラグインを介して WordPress. 。すべての主要なコンテンツ管理システム用のプラグインもあると確信しています。また、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"
これは、 header()
機能します Php
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>
オプション
ビリー・ムーンが推奨するように、何かがうまくいかない場合に備えてリフレッシュリンクを提供できます。
自動的にリダイレクトされていない場合: <a href='http://example.com/alternat_url.html'>Click here</a>
資力
頭の内側に配置された次のメタタグは、ブラウザにリダイレクトするように指示します。
<meta http-equiv="Refresh" content="seconds; url=URL">
秒をリダイレクトする前に待機する秒数に秒数を置き、URLをリダイレクトするURLに置き換えます。
または、JavaScriptでリダイレクトできます。これをページのどこにでもスクリプトタグの内側に配置します。
window.location = "URL"
最新のWeb標準に従うことを楽しみにしている場合は、平易なHTMLメタリダイレクトを避ける必要があります。サーバー側のコードを作成できない場合は、選択する必要があります JavaScriptリダイレクト 代わりは。
JavaScript-Disabledブラウザをサポートするには、HTMLメタリダイレクトラインをに追加します noscript
エレメント。 noscript
ネストされたメタリダイレクトと組み合わせた canonical
タグは、検索エンジンのランキングにも役立ちます。
リダイレクトループを避けたい場合は、 location.replace()
JavaScript関数。
適切なクライアント側 URLリダイレクト コードは次のように見えます(インターネットエクスプローラー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 -->
aを使用できます メタ "リダイレクト":
<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>
次のコードを次のように配置しますu003CHEAD>とu003C/HEAD>HTMLコードのタグ:
<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モバイル アプリケーションでは、場合によってはメタヘッダータグがリダイレクトを適切に実現しません(jQueryモバイルは各ページのヘッダーを自動的に読み取らないため、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>
JavaScriptを使用する必要があります。次のコードをヘッドタグに配置します。
<script type="text/javascript">
window.location.assign("http://www.example.com")
</script>
HTTPステータスコード301または302で自動リダイレクトできます。
PHPの場合:
<?php
Header("HTTP/1.1 301 Moved Permanently");
Header("Location: http://www.redirect-url.com");
?>
良い尺度のために:
<?php
header("Location: example@example.com", TRUE, 303);
exit;
?>
スクリプトの上にエコーがないことを確認してください。そうしないと、無視されます。http://php.net/manual/en/function.header.php
ボディタグのオンロードイベントを使用するだけです。
<body onload="window.location = 'http://example.com/'">
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>
かみそりエンジン 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>
これにはJavaScriptコードは必要ありません。これを書く <head>
HTMLページのセクション:
<meta http-equiv="refresh" content="0; url=example.com" />
ページが0秒でロードされるとすぐに、ページに移動できます。
私が理解している限り、この質問のためにこれまでに見たすべての方法は、歴史に古い場所を追加しているようです。ページをリダイレクトするには、歴史の中で古い場所がありません。 replace
方法:
<script>
window.location.replace("http://example.com");
</script>
これは、私が望んでいたものすべてを備えたリダイレクトソリューションですが、カットと貼り付けのための素敵なクリーンなスニペットで見つけることができませんでした。
このスニペットには多くの利点があります。
- ユーザーがURLに持っているクエリストリングパラメーションをキャッチして保持できます
- 不要なキャッシュを避けるために、リンクをqiueにします
- 古いサイトと新しいサイト名をユーザーに通知してみましょう
- 設定可能なカウントダウンを示します
- PARAMを保持するため、ディープリンクリダイレクトに使用できます
使い方:
サイト全体を移行した場合、古いサーバーで元のサイトを停止し、ルートフォルダーのデフォルトのindex.htmlファイルとしてこのファイルを使用して別のサイトを作成します。 404エラーがこのindex.htmlページにリダイレクトされるように、サイト設定を編集します。これにより、サブレベルのページなどへのリンクを持つ古いサイトにアクセスする人を捕まえます。
次に、オープニングスクリプトタグに移動し、OldSiteとNewsite 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>
このコードを使用してください:
<meta http-equiv="refresh" content="0; url=https://google.com/" />
注目してください: ヘッドタグに入れます。
JavaScriptでそれを行うことができます:
location = "url";