htaccess 301 redirect url


htaccess 301 redirect url

我有点进退两难。

我已经在一个项目上工作了将近一年,现在有一个旧域名。不知道谷歌如何在旧域名下索引整个网站,这让我很担心。我想把网站放在我的新域名上,我认为做301是前进的方向。我需要在每个动态页面上都这样做。好的是页面结构是相同的。

如果有什么建议的话,我将不胜感激。

最好的方法是创建一个.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>

这将把所有从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中。

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

在旧域的.htaccess中添加这样的行:

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

上面这行将把所有的url从旧域名重定向到新域名,使用301代码(永久重定向),这是从SEO的角度来看是正确的。

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

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

Apache mod_rewrite手册中有一页:何时不使用mod_rewrite。这里列出了这个特殊的场景(就资源而言,Redirect比rewriterrule轻得多,这在非常繁忙的服务器上可能是一个问题)。

有一种方法可以通过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();
?>