How To Redirect Page to Another Domain with .htaccess file


redirect page to another domain

Sometimes you may need to move a single page to a different domain. In such cases, if you do not redirect your old page to the new domain, then you will end up losing valuable website traffic. It will also adversely affect the search engine rankings for that page. To solve this problem, you can setup a permanent redirect (also known as 301 redirect) from your old page to the new domain using .htaccess file in server. Here’s how you can redirect page to another domain with .htaccess file, using the Redirect directive.

 

Redirect Page to Another Domain using Redirect directive

Redirect is a client-side request to make the web browser go to the new url. It is suitable for search engine optimization. In this case, the browser address bar will update to new URL. If you want to redirect without updating the displayed URL, checkout RewriteCond directive (below)

Open the .htaccess file using a text editor and paste the following command. Update the actual paths in the following command as per your requirement so .htaccess knows where to find your file.

Redirect 301 /path/to/old/file/old.html http://www.example2.com/new/file/new.html

Let’s look at the above command.

When we add 301 to a Redirect command it tells the server to permanently redirect old location to the new one.

 

First Path

The first path must be a relative UNIX path and not an absolute path. For example, if your .htaccess file is present in example.com folder then you should not include /home/sample_user/example.com in your first path. ‘/’ represents example.com directory.

 

Second Path

The second path is an absolute UNIX path to a different location on a new server, or even the same server.

 

 

Redirect Page to Another Domain using RewriteCond

You can also use the RewriteCond directive to redirect page to another domain. This is a server side directive. In this case, the URL in browser’s address bar won’t change. The server simply serves response from the new location.

The following command redirects http://www.example.com/file.html to another domain (www.example2.com). You can update the command as per your requirements.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^file\.html$ http://www.example2.com/file.html? [L,R=301]

 

Let’s look at the above commands.

The first line tells Apache to start the rewrite module.

The second line tells to Apache to apply this redirect only if the host is http://www.example.com

The third line tells Apache to specifically redirect file.html to a new domain http://www.example2.com

 

Redirect vs RewriteCond

When you use Redirect directive, the server first sends a redirect response to the client (browser). Then the client has to send a second request with the new URL. This time, the server will serve the new file location. This can be inefficient, since the client needs to make 2 requests. However, if you use a 301 with Redirect, then the client will automatically replace the old URL with the new one, when the old one is requested. In this case, it takes only 1 request.

When you use the RewriteCond, the server will rewrite your requested URL before processing it, so it takes only 1 request. However, it doesn’t change the URL in browser’s address bar. It only loads the response from its new location.

 

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!