How to redirect a domain

We’ll show you how to redirect a domain. URL redirection, also called URL redirect, is the World Wide Web technology for making a web page accessible under multiple URLs. When the web browser tries to open a URL that has been redirected, a page with a different URL will be opened. There are several ways to redirect a domain and it depends on the web server that is being used, etc. In this article, we will show you how to redirect domain with Apache webserver and redirect URL from Nginx webserver.

How to redirect to Apache web server

Apache HTTP Server is free and open source cross-platform web server software. 92% of Apache HTTPS server copies work on all Linux distributions.

Install Apache on your server if not already installed.

On RPM Linux distributions like CentOS and Fedora, use the following command to install Apache:

yum install httpd

Make sure mod_rewrite module is enabled:

httpd -M | grep rewrite
 rewrite_module (shared)

On Ubuntu and Debian, run the following command:

sudo apt-get update
sudo apt-get install apache2

Activate Apache module mod_rewrite:

sudo a2enmod rewrite

Restart the Apache service:

sudo service apache2 restart

Creating a simple virtual host in Apache

Creating a simple virtual host in Apache for the old domain, in which you will redirect to the new domain, which you can buy at this site: Use the RedirectPermanent directive to redirect the web client to a new URL:

<VirtualHost *:80>

ServerName old-domain.ru
ServerAlias www.old-domain.ru

RedirectPermanent / http://www.new-domain.ru/

# при необходимости добавьте директиву AccessLog для ведения журнала запросов, например :

CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

Restart the Apache server:

Restart the Apache service to apply the changes.

You can also redirect a domain name to another using the rewrite rules placed in the .htaccess file located in the root directory of the old domain’s domain name. Create a new .htaccess file and add the following rules to it:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^old-domain.ru [NC,OR]
RewriteCond %{HTTP_HOST} ^www.old-domain.ru [NC]
RewriteRule ^(.*)$ http://new-domain.ru/$1 [L,R=301,NC]

Redirect from Nginx web server

Nginx is a free and open source web server / software that can also be used as reverse proxy, load balancer, and HTTP cache. Most web servers use NGINX, very often as load balancing.

Stop Apache

Stopping Apache on the Server

service httpd stop

Disable Apache service

Disable Apache service to start automatically at boot (CentOS 7):

systemctl disable httpd

Installing NGINX on RPM Linux Distributions

Installing the Nginx web server. On RPM Linux distributions such as CentOS and Fedora, use the following commands:

yum install epel-release
yum install nginx
systemctl enable nginx
service nginx start

Installing NGINX on Ubuntu

On Ubuntu (and other Debian-based Linux distributions), run the following command:

sudo service apache2 stop
sudo apt-get remove --purge apache2 apache2-utils
sudo rm -rf /etc/apache2
sudo apt-get update
sudo apt-get install nginx

If you get a message that there are no Nginx packages available, install Nginx using the Nginx repository:

sudo apt-get install python-software-properties
sudo add-apt-repository ppa:nginx/stable
sudo apt-get update
sudo apt-get install nginx

Starting NGINX

Start the Nginx services with the following command:

sudo service nginx start

Configuring NGINX

Modifying the current Nginx server block about the old domain, or creating a new server block if it hasn’t been created yet. Add the following lines:

server {
  listen 80;
  server_name old-domain.ru www.old-domain.ru;
  return 301 http://www.new-domain.ru$request_uri;
}

Note that $ REQUEST_URI will listen and redirect to anything after the domain.

If you have an older version of Nginx (version 0.9.1 or lower) add the following lines:

server {
  listen 80;
  server_name old-domain.ru www.old-domain.ru;
  rewrite ^ http://www.new-domain.ru$request_uri? permanent;
}

Restarting NGINX

Remember to restart the Nginx service for the changes to take effect:

service nginx restart

Sidebar