Redirect old domain to new domain
To redirect an old domain to a new domain, use any of these three simple .htaccess techniques.
One-to-one redirect via mod_rewrite
The most robust/extensible redirect technique employs Apache’s mod_rewrite
. Here is an example that you can add to the root .htaccess file on your old domain:
# Redirect old to new
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} ^(www\.)?your-old-domain\.com [NC]
RewriteRule (.*) https://your-new-domain.com/$1 [R=301,L]
</IfModule>
Remember to replace the domain names with those of your own. Once in place, that code will redirect each URI request to the same URI on the new domain. For example:
http://your-old-domain.com/ => https://your-new-domain.com/
http://your-old-domain.com/whatever/ => https://your-new-domain.com/whatever/
http://your-old-domain.com/another/example.php => https://your-new-domain.com/another/page/example.php
Pretty much covers everything, and you customize with additional rules as desired (one of the benefits of using mod_rewrite
).
One-to-one redirect via mod_alias + Redirect
Here is a much simpler technique for redirecting each URI from the old domain to its equivalent URI on the new domain:
# Redirect old to new
<IfModule mod_alias.c>
Redirect 301 / http://example.com/
</IfModule>
Here we are using mod_alias
instead of mod_rewrite
. This gives you less control, but theoretically better performance (talking a few microseconds here), depending on configuration, et al. Remember to replace the domain name with that of your own.
ALL-to-one redirect via mod_alias + RedirectMatch
Last but not least, here is a technique for redirecting all URI requests on the old domain to the homepage of the new domain. To help visualize this:
http://your-old-domain.com/ => https://your-new-domain.com/
http://your-old-domain.com/whatever/ => https://your-new-domain.com/
http://your-old-domain.com/another/example.php => https://your-new-domain.com/
To implement this redirect, add this code snippet to your site’s root .htaccess file:
# Redirect old to new
<IfModule mod_alias.c>
RedirectMatch 301 / http://example.com/
</IfModule>
As before, remember to replace the domain name with that of your own. This technique is nearly identical to the one provided in the previous section. The only difference is that here we are using Apache’s RedirectMatch
directive instead of Redirect
. This small difference is enough to change how each URI is redirected. So test either method thoroughly to ensure that the redirects are happening according to your needs.
Also, if you want to redirect to some other page instead of the homepage, you can change the directive like so:
RedirectMatch 301 / http://example.com/whatever/page/you/want/
Important: it’s best to use the RedirectMatch
method only when all of the redirected URIs actually exist on the new domain. Otherwise, you’ll be getting a bunch of 404 “Not Found” errors, which is not optimal for your site’s SEO.