.htaccesshttp-redirect

How to 301 redirect an entire domain while preserving the path


I am redirecting one domain to another, but I want to preserve the path in the redirect. So for example, I want to visit www.example.com/services/education/page.html, but my redirect will bring them to www.new-example.com/services/education/page.html. What do I write in my .htaccess file to preserve the path "/services/education/page.html"?

Right now I have:

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

But I'm not sure if that works or not (Can't test yet as I am waiting for domain details etc). I just want to be sure when I put the site live. Is that right or am I way off base?

Thanks!


Solution

  • This should do it:

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