How To Permanently Redirect A Domain To Another Domain

Are you planning to permanently redirect an entire domain to another domain? Maybe you decided to buy another domain name for better SEO and name brand recognition. In any case, this article is what you are looking for. This article will teach you step-by-step how to permanently redirect a domain to another domain.

internet www
Image via pixabay.com

In this article, we assume that you will be using Apache web server for hosting your domains. Note: if you only want to redirect a single URL or just a few URLs, but not an entire site, you can read our article here about how to do that.

There are 2 main ways to permanently redirect one domain to another. The first way is to edit a .htaccess file adding 2 commands to it. The second way is to edit 1 or 2 Apache config files, and add similar commands.

Method 1: Redirect Site Via a .htaccess File

If you are hosting your website with a shared web host, most likely you will not be able to change server configurations. So, you will probably your best option will be to redirect a site via .htaccess.

Find the .htaccess file in the root (base) directory of your website. If you are using Linux, you can show all files including hidden ones with the command:

ls -la

If a .htaccess file does not exist, then create a blank file named .htaccess using a text editor. Make sure to add the dot as part of the file name.

Let’s assume that you own the domain example.com, and you want to completely redirect it to
anothersite.com

All you need to do is add the following lines of code to the .htaccess file, ideally at the top of the file for convenience.

RewriteEngine On
RewriteRule ^(.*)$ http://anothersite.com/$1 [R=301,L]

Just change anothersite.com to whatever new site you want to redirect to.

Method 2: Modify The Apache Config File(s) For Your Old Domain

This method would work well if you don’t have an .htaccess file, but you have the ability to change a server configuration. This method would also be good if you just prefer configuring a web server using the config files instead of an .htaccess file.

First: Change Into The /etc/apache2/sites-available/ directory by typing:

cd /etc/apache2/sites-available/

After doing the previous command, find the .conf file for your old website that you want to redirect from. Let’s assume that you own the domain example.com, and you want to completely redirect it to anothersite.com

Open the example.com.conf file using a text editor (and make sure to use sudo). You can actually use any text editor you like, but I will show you using vim

sudo vim example.com.conf

And then all you need to do is to paste in the following code into the file.

RewriteEngine On
RewriteRule ^(.*)$ http://anothersite.com/$1 [R=301,L]

I personally usually paste that code the end of the file, but actually you can paste it any where in the file as long as it is somewhere between the lines:

<VirtualHost *:80>

</VirtualHost>

Make sure to comment out any other redirects that might be in this file by adding a # before the lines with the other redirects.

You can download a sample example.com.conf file with proper 301 redirects that I made here

How To Permanently Redirect A Domain That Supports HTTPS

The above steps for Apache are assuming that your site only supports HTTP. If your site supports HTTPS, using an SSL certificate, then you will need to edit the SSL config file as well.

Open the file called example.com-le-ssl.conf
sudo vim example.com-le-ssl.conf

Next, add the following lines of code into the file.

RewriteEngine On
RewriteRule ^(.*)$ http://anothersite.com/$1 [R=301,L]

Make sure that those lines you added to the file are somewhere between the lines that say:

<VirtualHost *:443>

</VirtualHost>

After you are done, save the file, and then restart Apache by typing:
sudo service apache2 restart

You can download a sample example.com-le-ssl.conf file with proper 301 redirects that I made here.

Miscellaneous Suggestions About Redirecting With The Apache Config File(s)

If you know that your site always redirects to HTTPS, you might technically only need to edit the example.com-le-ssl.conf file. But, to just be be safe, in case your server gets messed up later and doesn’t redirect to HTTPS properly, it’s best to edit both the files example.com.conf and example.com-le-ssl.conf

Always make sure to restart Apache after editing the .conf files for the changes to come into effect by doing:
sudo service apache2 restart

Conclusion

As for the lines that you add either in the .htacess file, or the Apache config file(s):

RewriteEngine On
RewriteRule ^(.*)$ http://anothersite.com/$1 [R=301,L]

, if you are wondering what those commands mean, the R=301 means a permanent redirect, L tells Apache not to consider any other rules and to do this immediately. And the $1 basically will map all of the old URLS to correspond to the new one. So for example, example.com would redirect to anothersite.com, example.com/blog would redirect to anothersite.com/blog, example.com/wiki would redirect to anothersite.com/wiki, and so on.

You don’t actually have to understand those commands to use them. But, if you want to understand those 2 lines a little bit more, you can read more from Apache’s official documentation here and here.

Lastly, no matter whether you use method 1 or method 2, make sure to change anothersite.com with whatever site that you want to redirect to.

Did you learn a lot from this article? Do you have anything to add? Let’s discuss it in the comments below.

6 thoughts on “How To Permanently Redirect A Domain To Another Domain”

Leave a Reply

Your email address will not be published. Required fields are marked *