How To Redirect All Pages To New Domain in Apache

Last updated on August 10th, 2020 at 03:05 am

When you migrate website to another domain, you will need to redirect old domain to new domain. You will also need to redirect all URLs, pages to another domain. Here’s how to redirect all pages to new domain in Apache. You can use these steps to redirect old website to new website.

 

How To Redirect All Pages To New Domain in Apache

Here are the steps to redirect all pages to new domain in Apache. There are 2 ways to redirect all URLs to another domain in Apache – using server configuration and using .htaccess file. We will look at both these approaches separately.

 

Using Apache Server Configuration

In this case, we redirect all old domain to new domain by modifying Apache server configuration file.

 

1. Open Apache Server Configuration File

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

  • /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 Site to Another Domain

 

2. Redirect All Pages to Another Domain in Apache

Let’s say you want to redirect all pages of your website (e.g www.website.com) to another domain (e.g www.new-site.com), then add the following VirtualHost tags to your server configuration file.

<VirtualHost *:80>
ServerName www.website.com
Redirect 301 / http://www.new-site.com/
</VirtualHost>

<VirtualHost *:80>
ServerName www.new-site.com
. . .
</VirtualHost>

In the above code, we create 2 Virtual Hosts, one for old domain and new domain. We configure virtual host for website.com to redirect all requests to new-site.com (in bold)

Bonus Read : Top 10 SSL Certificate Providers for Website

 

3. Restart Apache Server

Restart Apache Server to apply changes.

Ubuntu/Debian:

$ sudo service apache2 start

CentOS/Fedora/Redhat:

$ service httpd restart

Bonus Read : How to Redirect Domain Without Changing URL

 

Using .htaccess/mod_rewrite

If you don’t have access to Apache server configuration, or don’t want to change Apache server configuration, then you can use .htaccess file to redirect all pages of your 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 website.com) in a text editor, NOT the .htaccess for new-site.com

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

Bonus Read : How to Configure SSL Certificate in Apache

 

2. Redirect All Pages to New Domain in Apache

Add the following lines to redirect your website (e.g website.com) to another website (e.g new-site.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://website.com/

 

Using RewriteRule

RewriteEngine on
RewriteCond "%{HTTP_HOST}"   "!^www\.website\.com" [NC]
RewriteCond "%{HTTP_HOST}"   "!^$"
RewriteRule "^/?(.*)"        "http://www.new-site.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.website.com to www.new-site.com.

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