301 Redirects & Redirections rules with .htaccess?

301 redirect htaccess : Many peoples looking for the easiest solution to redirect the URLs from the first domain to the second domain or redirecting the domain to a specific subdirectory without changing anything. In this article, we will learn about the easiest way of redirections with the help of .htaccess file.

301 Redirect htaccess & Redirections Rules

Making changes from the backend is quite easy nowadays with the help of several webs hosting control panels like cPanel, Plesk, Direct Admin, etc. In this article we are using cPanel to elaborate the process, you can use the below .htaccess code on any panel or any apache webserver.

Redirect ‘A’ Domain to ‘B’ Domain

Suppose if you are using abc.com domain presently but for some reason, you wanted to shift your website to somesh.com, but also you don’t want to broke the URLs. So, in this case, this method will help you out.

Example –
someshzade.com/index2 will redirect to somesh.com/index2

RewriteCond %{HTTP_HOST} ^abc.com
RewriteRule ^(.*) http://somesh.com/$1 [P]

Instead of the above example domain make sure you replace it with your domains.


Redirect Domain To Specific URL

This often the best solution I ever used to redirect expired domains or any specific niche related domain to a specific URL to generate some hits on the post. You might use this redirection code in multiple ways as per your requirements.

Example –
xyzmarketingpost.com will redirect to someshzade.com/marketing-tips

RewriteCond %{HTTP_HOST} ^abc.com
RewriteRule ^(.*) https://someshzade.com/marketing-tips[P]

In the above code place your domain which you want to redirect instead of xyzmarketingpost.com and the destination URL instead of someshzade.com/marketing-tips.


Redirect Entire Website 301 Redirect

If you want to redirect all the web pages to a specific domain name this method will help you out.

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

In the above example, all the links will be redirected to the newdomain.com


Re-directing an IP address

Suppose you are done with the setup of your website on a particular public IP address of the webserver, now you want to redirect all the users to the new primary domain, then for that, you can use this method.

Example –
11.11.11.11 will be redirected to somesh.com

RewriteCond %{HTTP_HOST} ^11\.11\.11\.11
RewriteRule (.*) http://somesh.com/$1 [R=301,L

Single URL 301 Redirect

Suppose if you are having a blog and you wanted to redirect the specific blog post to another URL or if you are having a website, and you want to redirect a specific page to another page you can use Single URL 301 Redirection to make things work for you.

Redirect 301 /previous-page.php http://www.somesh.com/xyz.php

In the above example, domain/previous-page.php will be redirected to http://www.somesh.com/xyz.php


Also, Read – How to increase session timeout in php?