How to Redirect a Domain

How to Redirect a Domain

We’ll show you, how to redirect a domain. URL redirection, also called URL forwarding, is a World Wide Web technique for making a web page available under more than one URL address. When a web browser attempts to open a URL that has been redirected, a page with a different URL is opened. There are few ways to redirect a domain and it depends on the web server used etc. In this tutorial we are going to show you, how to redirect a domain with Apache web server and URL redirection with NGINX web server.

How to Redirect a Domain with Apache web server

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

Install Apache on your server if it is not installed yet.

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

yum install httpd

Verify that mod_rewrite module is enabled:

httpd -M | grep rewrite
 rewrite_module (shared)

On Ubuntu and Debian, run:

sudo apt-get update
sudo apt-get install apache2

Activate the apache mod_rewrite module:

sudo a2enmod rewrite

Restart the Apache service:

sudo service apache2 restart

Create a simple virtual host in Apache

Create a simple virtual host in Apache for the old domain in which you redirect it to the new domain:
Use the Redirect Permanent directive to redirect the web client to the new URL:

<VirtualHost *:80>

ServerName old-domain.com
ServerAlias www.old-domain.com

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

# optionally add an AccessLog directive here for logging the requests e.g. :

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 a different one using rewrite rules placed in .htaccess file located in the document root directory of the old domain name. Create a new .htaccess file and add the following rules to it:

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

How to Redirect a Domain with NGINX web server

Nginx is free and open source web server/software, which can also be used as a reverse proxy, load balancer and HTTP cache.  A large fraction of web servers use NGINX, very often as a load balancer.

Stop Apache

Stop Apache on your server

service httpd stop

Disable Apache service

Disable Apache service to automaticaly start on boot (CentOS 7):

systemctl disable httpd

Install NGINX  on RPM Linux Distros

Install nginx web server. On RPM based Linux distributions, like CentOS and Fedora, use the following commands:

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

Install NGINX  on Ubuntu

On Ubuntu (and other Debian based Linux distributions), run:

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 receive a message that there is no nginx package available or so, install nginx using the nginx repository:

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

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

Start NGINX

Start the nginx service with the following command:

sudo service nginx start

Configure NGINX

Edit the current nginx server block about the old domain or create a new server block if it is not created yet.
Add the following lines:

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

Please note that $request_uri will listen for 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.com www.old-domain.com;
  rewrite ^ http://www.new-domain.com$request_uri? permanent;
}

Restart NGINX

Do not forget to restart the nginx service for the changes to take effect:
service nginx restart


Of course you don’t have to redirect a domain if you use one of our managed VPS hosting services, in which case you can simply ask our expert Linux admins to redirect a domain name for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post, on how to redirect a domain, please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

Leave a Comment