Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php htaccess 301重定向url_Php_.htaccess_Redirect - Fatal编程技术网

Php htaccess 301重定向url

Php htaccess 301重定向url,php,.htaccess,redirect,Php,.htaccess,Redirect,我有一个小难题 我已经在一个项目上工作了将近一年,现在有一个旧域名。不知道谷歌是如何在旧域名下为整个网站编制索引的,这让我很担心。我想把我的新域名上的网站,我认为做一个301是前进的方向。我需要在每个动态页面上执行此操作。好的是页面结构是相同的 如果您有任何关于最佳方法的建议,我们将不胜感激。最好的方法是创建一个.htaccess文件(如果您还没有),并将其粘贴到html文件夹中 <IfModule mod_rewrite.c> RewriteEngine on Rewri

我有一个小难题

我已经在一个项目上工作了将近一年,现在有一个旧域名。不知道谷歌是如何在旧域名下为整个网站编制索引的,这让我很担心。我想把我的新域名上的网站,我认为做一个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>

重新启动发动机
重写规则^old/page new/page[R=301,L]#可以根据需要多次复制和粘贴此规则

这会将所有内容从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中。

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

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

将该行放入旧域上的
.htaccess

Redirect 301 / http://www.newdomain.com/
上面这一行将使用301代码将所有URL从旧域重定向到新域(永久重定向),这从SEO的角度来看是正确的

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

Apache mod_重写手册有一页:。这里列出了这个特定的场景(重定向在资源方面比重写规则轻得多,这在非常繁忙的服务器上可能是一个问题)

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