.htaccess用于站点迁移

时间:2012-09-13 17:24:54

标签: apache .htaccess mod-rewrite rewrite

我们正在迁移到新的服务器结构,我需要一些htaccess的帮助。我是一名php程序员,绝不是阿帕奇专家。

这是一个简单的解释:

  • 我需要www.oldsite.com的根目录重定向到根目录www.newsite.com。

  • 特定目录www.oldsite / dir1重定向到新网站上的子域名,如newdomain.newsite.com

  • 其他所有www.oldsite.com/whatever/需要访问apps.newsite.com/whatever /

我现在拥有的是:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule / http://www.newsite.com [L,R=301]
RewriteRule (.*)dir1/ http://newdomain.newsite.com [L,R=301]
RewriteRule (.*)$ http://apps.newsite.com/$1 [L,R=301]

但它不能按我的意愿工作......

无论如何,任何和所有的帮助将不胜感激。

谢谢, -Orallo

2 个答案:

答案 0 :(得分:0)

您的第一次重写导致了您的问题,基本上重定向了所有内容。

这应该做你想要的:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^/?$ http://www.newsite.com [L,R=301]
RewriteRule ^/?dir1/(.*) http://newdomain.newsite.com/$1 [L,R=301]
RewriteRule ^/?(.*)$ http://apps.newsite.com/$1 [L,R=301]

我建议将这组重写规则放在该主机的实际Apache conf文件中并关闭AllowOverride,因为它会提供更好的性能。由于您实际上不再使用旧网站的Web目录,因此没有理由让Apache服务器查找该目录(以及可能仍然存在的任何其他子目录)来查找.htaccess文件。

答案 1 :(得分:0)

使用mod_alias,您可以将其放在server / vhost配置或htaccess文件中(位于oldsite.com 的文档根目录中):

Redirect 301 /dir1 http://newdomain.newsite.com
RedirectMatch 301 ^/(.+)$ http://apps.newsite.com/$1
RedirectMatch 301 ^/$ http://www.newsite.com

对于mod_rewrite,您需要从重写规则中删除前导斜杠,因为您在htaccess文件中使用它们:

RewriteEngine On
RewriteBase /
RewriteRule ^$ http://www.newsite.com [L,R=301]
RewriteRule ^dir1/(.*) http://newdomain.newsite.com/$1 [L,R=301]
RewriteRule ^(.+)$ http://apps.newsite.com/$1 [L,R=301]