How To Redirect Domain Without Changing URL

Last updated on September 21st, 2021 at 10:56 am

When you move your website to a new domain you need to redirect its pages. Many times, you need to redirect your web pages to new domain without changing their URLs. Here’s how to redirect domain without changing URL in Apache.

 

How To Redirect Domain Without Changing URL

Here are the steps to redirect domain without changing URL.

Please ensure that you have enabled mod_rewrite in your Apache web server configuration. Only then your .htaccess configuration will be applied by Apache server.

If you have enabled htaccess using mod_rewrite, you can skip to step 4.

 

1. Enable mod_rewrite

Open terminal and run the following command to enable mod_rewrite on Ubuntu/Debian systems.

$ sudo a2enmod rewrite

If mod_rewrite is already enabled, you will see an alert message.

Restart Apache web server

$ sudo systemctl restart apache2

Bonus Read : How to Configure SSL Certificate in Apache Web Server

 

2. Enable .htaccess in Apache Server

By default, Apache does not allow the use of .htaccess file. So open the default Apache server configuration file

$ sudo vi /etc/apache2/sites-available/000-default.conf

Add the following lines just before </VirtualHost> line.

<Directory /var/www/html>
   Options Indexes FollowSymLinks MultiViews
   AllowOverride All
   Require all granted
</Directory>

Restart Apache web server

$ sudo systemctl restart apache2

Bonus Read : How to Redirect to Another Domain with .htaccess

 

3. Create .htaccess file

Open terminal and create .htaccess file

$ sudo vi /var/www/html/.htaccess

Make sure to add the following line at the top of your .htaccess file

RewriteEngine on

 

Bonus Read : How to Set Apache Header Conditionally

 

4. Redirect Domain Without Changing URL

If you want to redirect visitors from your old domain (old-site.com) to new domain (new-site.com)without changing URL, add the following lines to your htaccess file.

RewriteEngineOn
RewriteCond%{HTTP_HOST} ^old-site.com RewriteRule ^(.*) http://new-site.com/$1 [P]

This will redirect URL (e.g old-site.com/product) from old domain to same url on new domain ( e.g new-site.com/product)

 

If you want to redirect domain to a different URL on new domain (e.g new-site.com/new-product), add the following line to your .htaccess file

RewriteCond%{HTTP_HOST} ^old-site.com RewriteRule ^(.*) http://new-site.com/new-product [P]

 

 

5. Restart Apache Server

Restart Apache web server

$ sudo systemctl restart apache2

 

Now your Apache web server will automatically redirect page to another domain without changing URL.

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