htaccess 301 重定向网址

发布于 2024-11-19 12:45:04 字数 163 浏览 0 评论 0 原文

我有一个小困境。

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

任何有关最佳方法的建议将不胜感激。

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. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

琉璃繁缕 2024-11-26 12:45:04

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

The best way is to create a .htaccess file if you don't have one already and stick it in your html folder.

<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>
微暖i 2024-11-26 12:45:04

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

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

您可以将该 mod_rewrite 代码放入 olddomain 的 .htaccess 中。

This will redirect everything from olddomain.com to 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.

风尘浪孓 2024-11-26 12:45:04

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

将这样的行放入旧域的 .htaccess 中:

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

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

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

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

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

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.

Put such line into your .htaccess on old domain:

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.

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):

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. 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).

幽蝶幻影 2024-11-26 12:45:04

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

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
//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();
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文