Redirect Subfolder to Subdomain in Apache using .htaccess

Add RSS feed to Reader and sync to Readwise.

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.

Comments

Leave a Reply

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