简体   繁体   English

htaccess 301 重定向 url

[英]htaccess 301 redirect url

I have a small dilemma.我有一个小难题。

I have been working on a project for almost a year now and had an old domain name.我已经在一个项目上工作了将近一年,并且有一个旧域名。 Not knowing how Google has indexed the whole site under the old domain which has worried me.不知道谷歌是如何在旧域名下索引整个网站的,这让我很担心。 I want to put the site up on my new domain name and I think doing a 301 is the way forward.我想把网站放在我的新域名上,我认为做 301 是前进的方向。 I will need to do this on every dynamic page.我需要在每个动态页面上执行此操作。 The good thing is the page structure is identical.好消息是页面结构是相同的。

Any advice on the best way to do this would be massively appreciated.任何有关执行此操作的最佳方法的建议将不胜感激。

The best way is to create a .htaccess file if you don't have one already and stick it in your html folder.最好的方法是创建一个.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>

This will redirect everything from olddomain.com to newdomain.com:这会将所有内容从 olddomain.com 重定向到 newdomain.com:

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

You can put that mod_rewrite code into your olddomain's .htaccess.您可以将该 mod_rewrite 代码放入旧域的 .htaccess。

If your hosting place does not support mod_rewrite (yep, there are such hosting companies) or it does not work for some strange and unknown reason, go with a simpler and widely available directive: Redirect or RedirectMatch .如果您的托管地点不支持 mod_rewrite(是的,有这样的托管公司)或者由于某些奇怪和未知的原因而无法正常工作, go 具有更简单且广泛可用的指令: RedirectRedirectMatch

Put such line into your .htaccess on old domain:将这样的行放入旧域上的.htaccess中:

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

The above line will redirect ALL URLss from old domain into new domain suing 301 code (Permanent Redirect) which is the right thing from SEO perspective.上面的行会将旧域中的所有 URL 重定向到新域中,使用 301 代码(永久重定向),从 SEO 的角度来看这是正确的。

If you need to redirect such a single page or point it to a specific new location, use such rule (the same as above just specifying exact URL):如果您需要重定向这样的单个页面或将其指向特定的新位置,请使用此类规则(与上面相同,只是指定确切的 URL):

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

Apache mod_rewrite manual has a page: When NOT to use mod_rewrite . Apache mod_rewrite 手册有一个页面: When NOT to use mod_rewrite This particular scenario is listed there (Redirect is much lighter that RewriteRule in terms of resources, which can be an issue on very busy servers).此处列出了此特定场景(Redirect 在资源方面比 RewriteRule得多,这在非常繁忙的服务器上可能是一个问题)。

There is a way to do it via php (.htaccess is the best), but this has come in handy for me once or twice.有一种方法可以通过 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();
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM