redirect site to another domain

How To Redirect Site To Another Domain in Apache

When you buy a new domain or move your website to another domain, you need to redirect your domain to another site. Here’s how to redirect site to another domain in Apache web server. For this, you can create domain redirect in server configuration or .htaccess file.

 

How To Redirect Site To Another Domain

Here are the steps to redirect site to another domain. There are multiple ways to redirect your domain to another domain, using Apache web server. We will look at each of these methods separately.

 

Using Apache Server Configuration

 

1. Open Apache Server Configuration File

Apache server configuration is located at one of the following locations, depending on your installation setup.

  • /etc/apache2/httpd. conf
  • /etc/apache2/apache2. conf
  • /etc/httpd/httpd. conf
  • /etc/httpd/conf/httpd. conf

Open Terminal and run the following command to open Apache Server configuration file.

$ sudo vi /etc/apache2/httpd. conf

Bonus Read : How to Redirect Domain without changing URL

 

2. Redirect Site to Another Domain

Let’s say you want to redirect your website (e.g www.example1.com) to another domain (e.g www.example2.com), then add the following VirtualHost tags to your server configuration file.

<VirtualHost *:80>
ServerName www.example1.com
Redirect 301 / http://www.example2.com/
</VirtualHost>

<VirtualHost *:80>
ServerName www.example2.com
. . .
. . .
</VirtualHost>

In the above code, we create 2 Virtual Hosts, one for each domain. We configure virtual host for example1.com to redirect all requests to example2.com (in bold)

Bonus Read : How to Set Apache Header Conditionally

 

3. Restart Apache Server

Restart Apache Server to apply changes.

Ubuntu/Debian:

$ sudo service apache2 start

CentOS/Fedora/Redhat:

$ service httpd restart

 

Using .htaccess/mod_rewrite

If you don’t want to change Apache server configuration, or don’t have access to it, then you can use .htaccess file to redirect domain to another domain.

Before you proceed, please ensure you have enabled mod_rewrite in your Apache server. Here are the steps to do it and setup .htaccess file for your website.

 

1. Open .htaccess file

Open .htaccess file for your website (e.g example1.com) in a text editor, NOT the .htaccess for example2.com

$ sudo vi /var/www/example1.com/.htaccess

Bonus Read : How to Enable mod_headers in Apache

 

2. Redirect Domain to Another Domain

Add the following lines to redirect your website (e.g example1.com) to another website (e.g example2.com). There are multiple ways to redirect domain to another domain using .htaccess. You can use any of them, they all give the same result.

Using Redirect

RewriteEngine on
Redirect 301 / http://example2.com/

 

Using RewriteRule

RewriteEngine on
RewriteCond "%{HTTP_HOST}"   "!^www\.example1\.com" [NC]
RewriteCond "%{HTTP_HOST}"   "!^$"
RewriteRule "^/?(.*)"        "http://www.example2.com/$1" [L,R,NE]

Bonus Read : How to Redirect Page to Another Domain using .htaccess

 

3. Restart Apache Server

Restart Apache Server to apply changes.

Ubuntu/Debian:

$ sudo service apache2 start

CentOS/Fedora/Redhat:

$ service httpd restart

That’s it! Now Apache will automatically redirect all requests from www.example1.com to www.example2.com.

Ubiq makes it easy to visualize data in minutes, and monitor in real-time dashboards. Try it Today!