htaccess 301重定向网址

时间:2011-07-08 17:24:53

标签: php .htaccess redirect

我有一个小困境。

我已经在一个项目上工作了将近一年,并且有一个旧的域名。不知道谷歌如何将旧网域下的整个网站编入索引,令我担心。我想把网站放在我的新域名上,我认为做301就是前进的方向。我需要在每个动态页面上执行此操作。好的是页面结构是相同的。

任何有关实现这一目标的最佳方法的建议都将受到大力赞赏。

4 个答案:

答案 0 :(得分:6)

最好的方法是创建一个.htaccess文件,如果你还没有,并将其粘贴在你的html文件夹中。

<IfModule mod_rewrite.c>
  RewriteEngine on

  RewriteRule ^old/page new/page [R=301,L] #can copy and paste this as many times as needed

</IFModule>

答案 1 :(得分:5)

这会将所有内容从olddomain.com重定向到newdomain.com:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,QSA,L]

您可以将mod_rewrite代码放入旧域名.htaccess。

答案 2 :(得分:2)

如果您的托管地点不支持mod_rewrite(是的,有这样的托管公司)或者由于一些奇怪和未知的原因它不起作用,请使用更简单且广泛使用的指令:Redirect或{{3 }}

将这一行放入旧域.htaccess

Redirect 301 / http://www.newdomain.com/

以上一行将所有URL从旧域重定向到新域名suing 301代码(永久重定向),这是从SEO角度来看正确的事情。

如果您需要重定向这样的单个页面或将其指向特定的新位置,请使用此类规则(与上面指定的确切URL相同):

Redirect 301 /help-shipping.html http://www.newdomain.com/help/shipping.php

Apache mod_rewrite手册有一个页面:RedirectMatch。这里列出了这个特定的场景(重定向是更轻,就资源而言是RewriteRule,这可能是非常繁忙的服务器上的问题)。

答案 3 :(得分:1)

有一种方法可以通过php(.htaccess是最好的)来做到这一点,但这对我来说已经派上用场了一两次。

<?php
//this can be set however you need it to be
$dynamic_redirect = "http://foo.com".$your_variable; 
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".$dynamic_redirect);
exit();
?>