几种网页转址与“301 永久搬移”的语法

转址的方式分为:

  • 302: 暂时搬移 (Temporarily Moved)
  • 301: 永久搬移 (Permanently Moved)

一般网页转址与“302 暂时搬移”

HTML

<head>
<meta http-equiv=refresh content="0;url=http://host.domain.tld/path/to/">
</head>

JavaScript

<script language="JavaScript">
<!--
  window.location.href = "http://host.domain.tld/path/to/";
//-->
</script>

PHP

<?php
  header("Location: http://host.domain.tld/path/to/");
?>

(转址前不可有任何资料输出)

Perl

#!/usr/bin/perl -w
print "Location: http://host.domain.tld/path/to/\n\n";

(转址前不可有任何资料输出)

Perl - 使用 CGI 模组

#!/usr/bin/perl -w
use CGI qw/:standard/;
my $CGI = CGI->new();
print $CGI->redirect(
  -location => "http://host.domain.tld/path/to/"
);

(转址前不可有任何资料输出)

301 永久搬移

“301 永久搬移”必须在 Server 的 Header 做宣告,因此没有 Client 端的 HTML 或 JavaScript 语法。

PHP

<?php
  header("HTTP/1.1 301 Moved Permanently");
  header("Location: http://host.domain.tld/path/to/");
?>

(转址前不可有任何资料输出)

Perl

#!/usr/bin/perl -w
print "Status: 301 Moved Permanently\n";
print "Location: http://host.domain.tld/path/to/\n\n";

(转址前不可有任何资料输出)

Perl - 使用 CGI 模组

#!/usr/bin/perl -w
use CGI qw/:standard/;
my $CGI = CGI->new();
print $CGI->redirect(
  -location => "http://host.domain.tld/path/to/",
  -status => 301,
);

(转址前不可有任何资料输出)

参考网页

  1. Status Code Definitions
  2. Redirect Checker | Check your Statuscode 301 vs 302
  3. Redirect Checker | InternetOfficer
  1. Using Mozilla Firefox Mozilla Firefox 14.0.1 on Windows Windows XP

    请问各要存成哪种副档名,html 的存成 .htm 或 .html 这个没问题,
    其他的呢,可以解说的再详尽一点吗,谢谢

  2. Using Google Chrome Google Chrome 28.0.1500.95 on Windows Windows 7

    非常实用

return top

%d 位部落客按了赞: