Secara umum, server dan web host dapat diatur secara langsung.301 redirect(即301 Lompatan),不需要额外写代码进行设置。
但是,如果你不想在服务器或虚拟主机上实现301重定向该怎么办呢?

我们以使用Z-blogPHP程序做的网站为例。
Z-blogPHP程序的网站主题使用的都是统一的头部模板文件header.php
header.php所在路径——根目录/zb_users/theme/主题id/template/
找到header.php文件后,在header.php中添加我们的代码即可!
第一种方式:
实现finchui.com重定向到www.finchui.com
这种方法常常用于新网站。
代码如下:
{php}
if (strpos($_SERVER['HTTP_HOST'], 'www.finchui.com') === false) {
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.finchui.com");
exit();
}
{/php}Metode kedua:
Tidak hanya mencapai finchui.com untuk melompat ke www.finchui.com.
Juga mengimplementasikan akses misalnya: finchui.com/zbp-theme/62.html, finchui.com/zbp-theme/ redirect ke www.finchui.com/zbp-theme/62.html dan www.finchui.com/zbp-theme/62.html.
Sederhananya, perubahan hanya mengubah nama domain utama, dan bagian sufiks URL akan tetap tidak berubah.
这种方法常常用于旧的网址已经被搜索引擎收录了许多地址,为了把旧域名的权重转移到新域名,才这么做。
代码如下:
{php}
$redirectHost = 'www.finchui.com;
if (strpos($_SERVER['HTTP_HOST'], $redirectHost) === false) {
$redirectURL = 'http://' . $redirectHost . $_SERVER['REQUEST_URI'];
header("HTTP/1.1 301 Moved Permanently");
header("Location: $redirectURL");
exit();
}
{/php}以上的例子中用的是我的网站域名,而我的网站只是做了简单的finchui.com跳转到www.finchui.com,不包含URL后缀。






Tambahkan teman