htaccess 301 Redirect Domain Without Changing URL

The point is very clear. You have just changed your domain name. Maybe you moved from domain.com to domain.in, .co.uk, .sg etc. Now you want to redirect all the traffic you get from old-site to the new-site. You want to keep the URL as it is (the address that comes after the domain name). It is equivalent to changing the whole domain name from olddomain.com to newdomain.com. For both these same cases, you can use the below code in your htaccess file. If you have used any other redirects, either comment or remove them.

Add this code just below “RedirectEngine on” Line with the right domain names.

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

Example 1: Consider the domain “website.in/hello-how-are-you/“,
which needs to be pointed to “website.com/hello-how-are-you/” You will use this code.

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

Example 2: Consider the domain “domain.com/hello-how-are-you/“,
which needs to be pointed to “example.com/hello-how-are-you/” You will use this code.

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

This code works with web servers that support htaccess. If you face any troubles while redirecting, kindly seek help from your hosting provider. Hosting control panels are always a combination of multiple servers. Some use NGINX with Apache (Siteground), others use Litespeed along with Apache (Hostinger).

Please note that our product recommendations are unbiased and targetted to be user-friendly. If you have any recommendations which isn't listed on our site, please feel free to contact us. We would be happy to review it.

Hey Don't Worry! The Email entered will NOT be used for sending NewsLetters. We respect your Privacy 🙂

guest
0 Comments
Inline Feedbacks
View all comments