How to redirect subdirectory to subdomain using htaccess

293

How to redirect subdirectory to subdomain using htaccess is a common question when you want to move your website from subdirectory to subdomain. Initially, we have hosted our PHP App in the subdirectory but many times it conflicts with the WordPress multisite setup. So we have decided to move the https://yuvayana.org/test permanently to https://exam.yuvayana.org. Now all URLs or the subdirectory are being redirected to the new subdomain. For a beginner or who doesn’t know about redirection and working with htaccess it’s a painful task. Because in search you may get the same code but you don’t know where to add that code.

This was achieved by the simple htaccess code

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/dir
RewriteRule (.*) https://sudomain.example.com/$1 [R=301]

Now the question is where do you have to put this code?

Well, open the subdirectory and put this code in the htaccess file. Remove all other existing codes of the subdirectory’s htacess file.

While adding this code replace

dir -> with your subdirectory

example.com -> with your actual domain

subdomain -> with your actual subdomain

R=301 indicates that I have permanently moved this.

From SEO point of view initially you may see a drop but after some time it will work as previously. Now I suggest you to add your subdomain to different Webmaster tools and setup the analytics etc.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.