在php中相对于url重定向301
我正在移动到一个新域名,我需要重定向到新域名的所有传入链接。所有的网页都在这两个网站上 (例如:www.oldsite.com/services.php和www.example.com/services.php都是活动的,完全相同。) 我想知道我的代码是否是SEO友好的,而不会弄乱.htaccess文件在php中相对于url重定向301,php,redirect,seo,Php,Redirect,Seo,我正在移动到一个新域名,我需要重定向到新域名的所有传入链接。所有的网页都在这两个网站上 (例如:www.oldsite.com/services.php和www.example.com/services.php都是活动的,完全相同。) 我想知道我的代码是否是SEO友好的,而不会弄乱.htaccess文件 <?php $referer = $_SERVER['SERVER_NAME']; $domain = "www.exemple.ca"; $uri = $_SERVER['REQUEST
<?php
$referer = $_SERVER['SERVER_NAME'];
$domain = "www.exemple.ca";
$uri = $_SERVER['REQUEST_URI'];
if(strpos($referer,$domain) === false) {
header( "HTTP/1.1 301 Moved Permanently" );
header( "Status: 301 Moved Permanently" );
header( "Location: http://www.exemple.ca$uri" );
exit(0);
} ?>
301
对搜索引擎优化非常友好。它向爬行器指示页面已永久移动。以下几点:
- 您可能想要
(请求的主机名),而不是HTTP\u主机
SERVER\u名称
- 假设您不使用任何子域,则无需使用strpos,我只需比较两个主机名,如果它们不相等,则重定向
- 您只需要一个301头
- htaccess将比使用PHP快得多,特别是如果您已经有了htaccess文件
<?php
$referer = $_SERVER['HTTP_HOST'];
$domain = "www.exemple.com";
$uri = $_SERVER['REQUEST_URI'];
if($referer != $domain) {
header( "Location: http://www.exemple.ca$uri" true, 301);
exit(0);
} ?>
通过htaccess(两行)实现这一点要容易得多。感谢大家提供的这些提示
RewriteEngine On
RewriteCond %{HTTP_HOST} !(^www.exemple.ca)
RewriteRule (.*) http://www.exemple.ca/$1 [R=301,L]