[PHP][HTML][JS] 網頁自動轉址/導向頁面範例 301 & 302

HTML

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

Javascript

302 Found 效果

<script>location.href="http://code.yidas.com";</script>

301 Moved Permanently 效果 (Replace history.state)

<script>location.replace("http://code.yidas.com");</script>

PHP

302 Found - 暫時轉址

header('Location: https://code.yidas.com');

預設就是302

301 Moved Permanently - 永久轉址

header("HTTP/1.1 301 Moved Permanently");
header("Location: https://code.yidas.com");

301轉址只能為Web Server端註記

自定義

// 307 Temporary Redirect
header("Location: https://code.yidas.com", TRUE, 307);

Leave a Reply

Your email address will not be published. Required fields are marked *