{"id":24837,"date":"2018-01-11T02:30:54","date_gmt":"2018-01-11T08:30:54","guid":{"rendered":"https:\/\/www.rosehosting.com\/blog\/?p=24837"},"modified":"2022-06-03T03:35:19","modified_gmt":"2022-06-03T08:35:19","slug":"how-to-redirect-a-domain","status":"publish","type":"post","link":"https:\/\/www.rosehosting.com\/blog\/how-to-redirect-a-domain\/","title":{"rendered":"How to Redirect a Domain"},"content":{"rendered":"
<\/p>\n
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.
\n<\/p>\n
The Apache HTTP Server, is free and open-source cross-platform web server software.\u00a092% of Apache HTTPS Server copies run on Linux distributions.<\/p>\n
On RPM based Linux distributions, like CentOS and Fedora, use the following command to install Apache:<\/p>\n
yum install httpd<\/pre>\nVerify that mod_rewrite module is enabled:<\/h3>\n
httpd -M | grep rewrite\r\n rewrite_module (shared)<\/pre>\nOn Ubuntu and Debian, run:<\/h3>\n
sudo apt-get update\r\nsudo apt-get install apache2<\/pre>\nActivate the apache mod_rewrite module:<\/h3>\n
sudo a2enmod rewrite<\/pre>\nRestart the Apache service:<\/h3>\n
sudo service apache2 restart<\/pre>\nCreate a simple virtual host in Apache<\/h3>\n
Create a simple virtual host in Apache for the old domain in which you redirect it to the new domain:
\nUse the Redirect Permanent directive to redirect the web client to the new URL:<\/p>\n<VirtualHost *:80><\/p>\n
ServerName old-domain.com
\nServerAlias www.old-domain.com<\/p>\nRedirectPermanent \/ http:\/\/www.new-domain.com\/<\/p>\n
# optionally add an AccessLog directive here for logging the requests e.g. :<\/p>\n
CustomLog ${APACHE_LOG_DIR}\/access.log combined<\/p>\n
<\/VirtualHost><\/p>\n
Restart the Apache server:<\/h3>\n
Restart the Apache service to apply the changes.<\/p>\n
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:<\/p>\n
RewriteEngine on\r\nRewriteCond %{HTTP_HOST} ^old-domain.com [NC,OR]\r\nRewriteCond %{HTTP_HOST} ^www.old-domain.com [NC]\r\nRewriteRule ^(.*)$ http:\/\/new-domain.com\/$1 [L,R=301,NC]<\/pre>\nHow to Redirect a Domain with NGINX\u00a0web server<\/h2>\n
Nginx is free and open source web server\/software, which can also be used as a reverse proxy, load balancer and HTTP cache.\u00a0 A large fraction of web servers use NGINX, very often as a load balancer.<\/p>\n
Stop Apache<\/h3>\n
Stop Apache on your server<\/p>\n
service httpd stop<\/pre>\nDisable Apache service<\/h3>\n
Disable Apache service to automaticaly start on boot (CentOS 7):<\/p>\n
systemctl disable httpd<\/pre>\nInstall NGINX\u00a0\u00a0on RPM Linux Distros<\/h3>\n
Install nginx web server. On RPM based Linux distributions, like CentOS and Fedora, use the following commands:<\/p>\n
yum install epel-release\r\nyum install nginx\r\nsystemctl enable nginx\r\nservice nginx start<\/pre>\nInstall NGINX\u00a0 on Ubuntu<\/h3>\n
On Ubuntu (and other Debian based Linux distributions), run:<\/p>\n
sudo service apache2 stop\r\nsudo apt-get remove --purge apache2 apache2-utils\r\nsudo rm -rf \/etc\/apache2\r\nsudo apt-get update\r\nsudo apt-get install nginx<\/pre>\nIf you receive a message that there is no nginx package available or so, install nginx using the nginx repository:<\/p>\n
sudo apt-get install python-software-properties\r\nsudo add-apt-repository ppa:nginx\/stable\r\nsudo apt-get update\r\nsudo apt-get install nginx<\/pre>\nStart NGINX<\/h3>\n
Start the nginx service with the following command:<\/p>\n
sudo service nginx start<\/pre>\nConfigure NGINX<\/h3>\n
Edit the current nginx server block about the old domain or create a new server block if it is not created yet.
\nAdd the following lines:<\/p>\nserver {\r\n listen 80;\r\n server_name old-domain.com www.old-domain.com;\r\n return 301 http:\/\/www.new-domain.com$request_uri;\r\n}<\/pre>\nPlease note that $request_uri will listen for and redirect to anything after the domain.<\/p>\n
If you have an older version of nginx (version 0.9.1 or lower) add the following lines:<\/p>\n
server {\r\n listen 80;\r\n server_name old-domain.com www.old-domain.com;\r\n rewrite ^ http:\/\/www.new-domain.com$request_uri? permanent;\r\n}<\/pre>\nRestart NGINX<\/h3>\n
Do not forget to restart the nginx service for the changes to take effect:
\nservice nginx restart<\/p>\n
\nOf course you don\u2019t have to redirect a domain if you use one of our managed VPS hosting service<\/a>s, in which case you can simply ask our expert Linux admins to redirect a domain name<\/strong> for you. They are available 24×7 and will take care of your request immediately.<\/p>\n
PS<\/span>.<\/strong> 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.<\/p>\n","protected":false},"excerpt":{"rendered":"
We’ll show you, how to redirect a domain. URL redirection, also called URL forwarding, is a World Wide Web technique … <\/p>\n