我正在移动到一个新的域名,我需要将所有传入的链接重定向到新的域名。所有的页面都在两个网站上。
(例如: www.oldsite.com/services.php和www.exemple.com/services.php都是活跃且相同的。)
我想知道,如果我的代码是搜索引擎优化友好的,而不是混乱的.htaccess文件。
<?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);
} ?>发布于 2013-03-01 01:22:46
以下是几点:
PHP你可能想要PHP(请求的主机名),而不是SERVER_NAME.
strpos,我只需要比较两个主机名,如果它们不相等就重定向
考虑到这一点,我可能会将您的代码重写为:
<?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);
} ?>您可能也只想对GET请求执行此操作。
htaccess版本(假设启用了mod_rewrite )为:
RewriteEngine On
RewriteCond %{HTTP_HOST} !(^www.exemple.ca)
RewriteRule (.*) http://www.exemple.ca/$1 [R=301,L]发布于 2013-03-01 01:19:51
301对搜索引擎优化是最友好的。它向爬行器表明页面已永久移动。
https://stackoverflow.com/questions/15141675
复制相似问题