【问题标题】:301 redirect with .htaccess from old domain to a new301 使用 .htaccess 从旧域重定向到新域
【发布时间】:2013-09-23 06:20:18
【问题描述】:

有 old.test.ru 和 test.ru 域。如何将所有页面从 old.test.ru/* 重定向到 test.ru/*?

我有这些 .htaccess:

<IfModule mod_rewrite.c>
   Options +FollowSymLinks
   RewriteEngine On
   RewriteBase /

   # redirect from index.(php|asp|aspx|html|htm), main.(php|asp|aspx|html|htm), home.(php|asp|aspx|html|htm)
   RewriteRule ^(.*)index\.(php|asp|aspx|html|htm)$ $1 [R=301,L]
   RewriteRule ^(.*)main\.(php|asp|aspx|html|htm)$ $1 [R=301,L]
   RewriteRule ^(.*)home\.(php|asp|aspx|html|htm)$ $1 [R=301,L]

   # redirect from dev and www
   RewriteCond %{HTTP_HOST} ^old\.test\.ru$ [OR]
   RewriteCond %{HTTP_HOST} ^www\.test\.ru$
   RewriteRule ^(.*)$ http://test\.ru/$1 [R=301,NC]
</IfModule>

我认为最后一行

RewriteRule ^(.*)$ http://test\.ru/$1 [R=301,NC]

应该可以,但是不行。

适用于:

old.test.ru/about.php -> test.ru/about.php
old.test.ru/company.php -> test.ru/company.php
old.test.ru/contact.php -> test.ru/contact.php

但不适用于:

old.test.ru/some/about.php -> test.ru/some/about.php
old.test.ru/some1/company.php -> test.ru/some1/company.php
old.test.ru/some2/contact.php -> test.ru/some2/contact.php
old.test.ru/some2/subsome/contact.php -> test.ru/some2/subsome/contact.php

我应该改变什么?

【问题讨论】:

标签: apache .htaccess mod-rewrite


【解决方案1】:

尝试用这个替换你的最后一条规则:

 # redirect from dev and www
 RewriteCond %{HTTP_HOST} ^(www|old)\.test\.ru$ [NC]
 RewriteRule ^ http://test.ru%{REQUEST_URI} [R=301,L]

更新:要从 URL 中删除 index.php 等:

 RewriteCond %{HTTP_HOST} ^(www|old)\.test\.ru$ [NC]
 RewriteRule ^(.*?)((?:index|default)\.(?:php|html?))?$ http://test.ru/$1 [R=301,L,NC]

【讨论】:

  • 不幸的是,它不起作用......现在甚至使用 old.test.ru 重定向到 test.ru 停止工作......
  • 确保将此代码放在您拥有的任何其他规则之前。还告诉我你的 Apache 版本是什么?
  • 服务器版本:Apache/2.2.23 (FreeBSD) 服务器构建时间:2012 年 11 月 10 日 20:55:13
  • 好的,将此代码放在RewriteBase 行下方并在不同的浏览器中测试。
  • .htaccess: &lt;IfModule mod_rewrite.c&gt; Options +FollowSymLinks RewriteEngine On RewriteBase / RewriteRule ^(.*)index\.(php|asp|aspx|html|htm)$ $1 [R=301,L] RewriteRule ^(.*)main\.(php|asp|aspx|html|htm)$ $1 [R=301,L] RewriteRule ^(.*)home\.(php|asp|aspx|html|htm)$ $1 [R=301,L] # redirect from dev and www RewriteCond %{HTTP_HOST} ^dev\.test\.ru$ [OR] RewriteCond %{HTTP_HOST} ^www\.test\.ru$ RewriteRule ^(.*)$ http://test\.ru/$1 [R=301,NC] &lt;/IfModule&gt;
【解决方案2】:

如果您想重定向所有请求,我会在 old.test.ru 上使用 Redirect 而不是使用 mod_rewrite

old.test.ru 的配置中使用:

Redirect permanent / http://test.ru/

【讨论】:

  • 它也不起作用 - 出现错误:在此页面上,发现循环转发。
  • 你必须清除你的RewriteRule,在你的.htaccess文件中从old.test.ru重写到test.ruRewriteCond %{HTTP_HOST} ^old\.test\.ru$ [OR].
  • 不明白什么意思?
  • 我假设你在httpd.conf 中有一个&lt;VirtualHost&gt; 或类似的old.test.ru 在这个配置中你放置了重定向。此外,您不需要在.htaccess 中进行重定向,因为它已经处理好了。查看我提供的链接,并在其中进行了解释。 old.test.ru 是否只是 Aliastest.ru
猜你喜欢
  • 2016-02-22
  • 1970-01-01
  • 1970-01-01
  • 2020-06-15
  • 2011-09-23
  • 2016-06-24
  • 2011-11-04
  • 2017-06-30
  • 1970-01-01
相关资源
最近更新 更多