How To Redirect Page To Another Domain With .htaccess File

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

When you move your website to a new domain, you need to redirect pages to another domain. Here’s how to easily redirect page to another domain with .htaccess file in Apache web server. There are multiple ways to redirect page to another domain. We will look at each of them.

 

How To Redirect Page To Another Domain With .htaccess File

Here are the steps to redirect page to another domain with .htaccess file. 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 Disable Apache Cache

 

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 Enable mod_ssl in Apache

 

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

 

4. Redirect Page to Another Domain using .htaccess file

Let’s say you want to redirect page /product.html to another domain www.newsite.com. There are multiple ways to redirect page to another domain using .htaccess file.

 

Redirect Page to Another Domain using Redirect

Add the following Redirect directive to redirect your page to another domain. Redirect statement requires 2 arguments – source URL path and destination URL path. You can use either absolute URL paths or relative URL paths

Redirect /product.html http://www.newsite.com/product.html

If you want to permanently redirect page to another domain, just add 301 after Redirect directive, as shown

Redirect 301 /product.html http://www.newsite.com/product.html

If you want to redirect all pages to new domain root, just add the following line to .htaccess

Redirect 301 / http://www.newsite.com/

Redirect directive will redirect all URLs that begin with the URL prefix (e.g /product.html) you specified. Redirect will also redirect URLs with parameters to new domain.

Bonus Read : How to Redirect 404 to Homepage using .htaccess

 

Redirect Page to Another Domain using RedirectMatch

You can also use RedirectMatch directive to redirect your page to another domain.

Redirect statement will redirect pages that begin with specified URL prefix but RedirectMatch will redirect all pages that match a specified Regular expression

RedirectMatch statement requires 2 arguments – regular expression to match and destination URL path.

RedirectMatch ^/product.html http://www.newsite.com/product.html

If you want to permanently redirect page to another domain, just add 301 after RedirectMatch directive, as shown

RedirectMatch 301 ^/product.html http://www.newsite.com/product.html

RedirectMatch directive will redirect all URLs that begin with the Regex (e.g /product.html) you specified. RedirectMatch will also redirect URLs with parameters to new domain

Also Read : How to Password Protect Directory in Apache

 

5. Restart Apache Server

Restart Apache web server

$ sudo systemctl restart apache2

 

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

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