[精讚] [會員登入]
13706

網頁自動轉址的四種方法

設定網頁自動轉址的四種方法,三種html,一種php

分享此文連結 //n.sfs.tw/10419

分享連結 網頁自動轉址的四種方法@新精讚
(文章歡迎轉載,務必尊重版權註明連結來源)
2020-01-08 11:38:47 最後編修
2016-12-12 10:28:28 By 張○○
 

自動目錄

要自動轉址有幾種方法:

1. 用 body onload 的方法

<body onLoad="window.location='http://n.sfs.tw'">

2. 用 header  meta 的方法 (建議)

<head>
<meta http-equiv="refresh" content="0;url=http://n.sfs.tw">
</head>

這種方法的好處是可以設定轉址秒數,例如設定每5分鐘重新 reload(轉址給自己)
<meta http-equiv="refresh" content="300" />

或是5秒後轉址
<meta http-equiv="refresh" content="5;url=http://n.sfs.tw" />

3. 用 javascript 的方法

<head>
<script>
  location.href= ('http://n.sfs.tw');
</script>
</head>

4. php 用 header 的方法

  header("Location: http://n.sfs.tw/";;;);

  header('Location: http://n.sfs.tw/ ', true, 302);  //說明跳轉原因,true 代表刷新之前的header(例如需要送多組header時,302 代表暫時跳轉

  header("Refresh: 0; url=http://n.sfs.tw/") ; //也是不錯的寫法,可以指定刷新時間(0是立即),單位是秒。

* 特別注意,除非萬不得已,不要輕易使用轉址,因為使用轉址會影響到 SEO。例如你使用的主機商預設只有 index.html,你要用 index.php;,而你又沒辦法修改 DocumentRoot ,只好使用轉址。

參考資料

[1] PHP 官網 http://php.net/manual/en/function.header.php


原文 2010-04-23 20:58:24

END

你可能感興趣的文章

[HTML] 影像地圖 使用HTML的MAP標籤製作影像地圖

[CSS] HTML 畫出粗外框表格的方法 (粗框表格) 在畫表格的時候,習慣上表格的外框要比內部的線條來得粗,不但比較美觀,而且看起來比較完整

[HTLM5] 表單number和range輸入類型 介紹HTML5和數字有關的輸入類型number/range 等類型

[HTLM5] html元件上的 data-* 屬性 html5 中元件中多了一個 data-*的屬性,這個屬性超級好用

UTF-8 BOM (Byte Order Mark) 的問題 在 Michael Kaplan 那看到 Every character has a story #4: U+feff

[CSS] 背景 background css 中最常用的特性(property)之一就是 background ,這個有什麼好難的?事實上 backgroun

隨機好文

[bc] linux 的計算機 bc 設定小數位數、計算π、次方根 linux 的計算機 bc 設定小數位數、計算π、次方根

Smarty安裝 smarty 是著名的樣版引擎,非常的好用,用多了突然發現拿掉smarty反而不會寫php了,以下是安裝過程..

設計的工作絕不接受比價 拿買陽春麵的價格想買牛肉麵,寧願倒掉也不賣

[Wildfly10] 發佈war檔 deploy war file onto wildfly10

[MAC] 安裝APACHE+PHP OS Darwin OpenIDMac1deAir 15.6.0 Darwin Kernel Version 15.6.