[Fabio M Costa] Domain redirect keeping the URL path with .htaccess

I had a site hosted at a domain and the client asked me to change the domain. But it’s a very bad idea to simply change a site’s address without thinking about all the users who have bookmarked the old site. It means losing all of them. So I thought that i could do a redirect from the old domain to the new one. This redirect should be using HTTP status code 301 to identify that the site has moved permanently. There are many ways to do such a thing. But one took my attention. Why not use Apache .htaccess file associated with mod_rewrite? Most of the hosting services supports this. Firstly I tried this:

RewriteEngine On
RewriteBase /
Redirect 301 / http://mynewdomain.com/

This way, all requests to the root of my old domain get redirected to the root of my new domain. But what if I try yo access some path inside my old domain? It would raise a 404 error because it does not have the content anymore. I want that if the user access http://myolddomain.com/path/ it redirects him to http://mynewdomain.com/path/. So I ended up with this:

RewriteEngine On
RewriteBase /
RedirectMatch 301 (.*)$ http://mynewdomain.com$1