如何301在保留路径的同时重定向整个域

时间:2011-12-09 19:18:46

标签: .htaccess redirect

我将一个域重定向到另一个域,但我想保留重定向中的路径。例如,我想访问www.example.com/services/education/page.html,但我的重定向会将它们带到www.new-example.com/services/education/page.html。我在.htaccess文件中写什么来保留路径“/services/education/page.html”?

现在我有:

redirect 301 http://www.example.com/ http://www.new-example.com/

但我不确定这是否有效(在等待域名详情等时无法测试)。我只想确定何时将网站直播。是的还是我离开基地了?

谢谢!

3 个答案:

答案 0 :(得分:28)

这应该这样做:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !new-example.com$ [NC]
RewriteRule ^(.*)$ http://new-example.com/$1 [L,R=301]

答案 1 :(得分:8)

尝试将以下内容添加到example.com域根目录中的.htaccess

RewriteEngine On
RewriteBase /

#for all requests to www.example.com
RewriteCond %{HTTP_HOST} ^www\.example\.com$
#redirect them to new-example
RewriteRule (.*) http://www.new-example.com/$1 [R=301,L]

答案 2 :(得分:6)

您的原始命令使用mod_alias Apache模块,它可以工作,但您可能希望将其更新为:

Redirect 301 / http://www.new-example.com/

删除当前(旧)域的确切域意味着指向该文件夹的所有域都将被发送到新域,从而使该单行脚本更加健壮。

其他答案使用mod_rewrite Apache模块。如果你也安装了它,那就可以使用了,尽管与一个代码相比,它有4行代码。此外,mod_alias是“基础”包的一部分,因此应该在所有Apache服务器上,而mod_rewrite是可选扩展,因此有些可能没有。

相关问题