How to Redirect Directory to Another Domain Using .htaccess


redirect subdomain to another domain

When you buy a new domain, you are very likely to move content from one or more folders on your old site to the new one. In such cases, it’s essential to set up redirect directory to another domain using .htaccess file on your old site. This will ensure that search engines and site visitors know that your directory has moved to a new domain.

 

How to Redirect Directory to Another Domain Using .htaccess

If you want to redirect directory (e.g /blog) to another domain (e.g new-domain.com/blog) using .htaccess, paste the following command


RedirectMatch 301 ^/blog/(.*)$ http://new-domain.com/blog/$1

 

Using Redirect directive


Redirect 301 /blog http://www.new-domain.com/blog

 

Using RewriteRule


RewriteRule ^/blog/(.*)$ http://new-domain.com/blog/$1 [R=301,L]

 

 

Redirect Directory to Another Directory in different domain

If you want to redirect directory (e.g /blog) to another domain (e.g new-domain.com/articles) using .htaccess, paste the following command


RedirectMatch 301 ^/blog/(.*)$ http://new-domain.com/articles/$1

 

Using Redirect directive


Redirect 301 /blog http://www.new-domain.com/articles

 

Using RewriteRule


RewriteRule ^/blog/(.*)$ http://new-domain.com/articles/$1 [R=301,L]

 

Redirect Directory to Another Directory in same domain

If you want to redirect directory (e.g /blog) to another domain (e.g /articles) using .htaccess, paste the following command


RedirectMatch 301 ^/blog/(.*)$ /articles/$1

 

Using Redirect directive


Redirect 301 /blog /articles

 

Using RewriteRule


RewriteRule ^/blog/(.*)$ /articles/$1 [R=301,L]

 

Make sure to add these 2 lines at the top of .htaccess before you paste any redirect commands in it.


Options +FollowSymLinks
RewriteEngine On

 

RewriteMatch vs Redirect

If you are wondering whether to use RewriteMatch or Redirect, remember that RewriteMatch matches the regular expression ( e.g ^/blog/(.*)$ ) while Redirect matches URLs starting with the given match string. That is why you use RegEx meta characters such as $, ^ in RedirectMatch.

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!