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

jas*_*ton 17 .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/
Run Code Online (Sandbox Code Playgroud)

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

谢谢!

Eri*_*del 31

这应该这样做:

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


Ulr*_*lha 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]
Run Code Online (Sandbox Code Playgroud)


Mid*_*ing 7

您的原始命令使用mod_aliasApache模块,虽然您可能希望将其更新为:

Redirect 301 / http://www.new-example.com/
Run Code Online (Sandbox Code Playgroud)

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

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