How to properly redirect old urls to new domain

If you change your domain, you can either use DNS or HTTP/Apache configuration to point the old content to the new location.

It is recommended to use the DNS configuration, because it is more efficient for such tasks.

However, In this short article, you’ll find an example of how to achieve that with simple .htaccess file rewrite. By doing that, we instruct the HTTP server (Apache, nginx, etc) to redirect the requests.

If you want to redirect the whole url structure to the new domain, you can add this to your .htaccess file, in the root directory with the old domain:

RewriteEngine on
RewriteCond %{HTTP_HOST} [NC]
RewriteRule ^(.*) [R=301,L]

That will conserve all the appended strings after the first trailing slash.

Or you can exclude from the redirect some urls or files, by adding this to the above snippet:

RewriteCond %{REQUEST_URI} !^/doNotRedirectThis.html$

Or you can simply redirect every request to the root of the new domain, like this:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*) [R=301,L]

This way, no matter what we request, we will land on the home page of the new domain.