Redirect Subfolder to Subdomain using .htaccess

Setup redirection after migrating a site from a subfolder to a subdomain.

When a site in a subfolder is moved to a subdomain, .htaccess rules need to updated to redirect URLs. This can be done by adding the following lines to the .htaccess file in the root folder:

RewriteCond %{HTTP_HOST} ^domain\.com$ [OR] RewriteCond %{HTTP_HOST} ^www\.domain\.com$ RewriteRule ^subfolder\/?(.*)$ "https\:\/\/subdomain\.domain\.com\/$1" [R=301,L]
Code language: JavaScript (javascript)

This will redirect all calls to domain.com/subfolder/* to subdomain.domain.com/* where * indicates all the subpages under the subfolder.

Couple of points to note for this to work:

  • The .htaccess rewrite rules work by the line order in the file. So these rules need to be above other rules for it to work.
  • On WordPress sites, place these lines above the WordPress redirect rules, i.e., above the # BEGIN WordPress line.
  • The .htaccess files in the subfolder need to be removed.

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *