How to add 301 permanent redirect using .htaccess?

Most of times we stuck in getting out of situations where it demands some kind of redirects whether its permanent redirect or a kind of temporary redirect (302).

Permanent Redirect:

If you need to change the URL of a page as it is shown in search engine results, we recommend that you use a server-side 301 redirect. This is the best way to ensure that users and search engines are directed to the correct page. The 301 status code means that a page has permanently moved to a new location.

Source: www.google.com

To add a 301 redirect or a permanent redirect You can write the below code in Your website’s .htaccess file:

Redirect 301 /oldpage.php /newpage.php 

or

Redirect 301 /oldpage.php http://gyansthali.in/newpage.php    

Now lets have a scenario where you want to add one or many 301 redirects

e.g.1: http://xyz.com/abc to http://abcdef.com/abcr.php
e.g.2: http://xyz.com/pqr to http://abcdef.com/apqr.php

Here in this case domain xyz.com is a new domain not parked anywhere or is without any associated hosting account and a complete website is running at domain abcdef.com

So in order to add 301 cross domain redirect from http://xyz.com/abc to http://abcdef.com/abcr.php

Please follow the steps as mentioned below:

Add your new domain to any hosting account and point it to a blank folder (lets say blank folder name is  xyz). 
Now update the domain xyz.com A record to point to the hosting account
Create a file with the name .htaccess inside the folder xyz
Add the below lines in your .htaccess file

Redirect 301 /abc to http://abcdef.com/abcr.php

Now, to add multiple redirects, just copy the above lines and replace it with appropriate web page urls.
Good Luck 🙂

Related Post