Donald Torres
Donald Torres

Reputation: 1

301 redirect all subfolders in subdomain

My site has a subdomain: docs.example.com and many folders within: docs.example.com/math, docs.example.com/science, docs.example.com/language and so on. All these URLs are indexed in Google. I need the subdomain and its subfolders to be redirected to the main URL: www.example.com.

I need to do a 301 redirect because of SEO. I have access to .htaccess in the host server. I tried this:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^docs.example.com$
RewriteRule ^(.*)$ http://www.example.com/ [R=301,L]

Works fine for docs.example.com but for docs.example.com/math I get a 404 error. Everything beneath docs.example.com MUST be 301 redirected, all subfolders and files.

Thanks for your help in advance

Upvotes: 0

Views: 178

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133538

Please following htaccess rules at top of your .htaccess Rules file. Please make sure to clear your browser cache before testing your URLs.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:www\.)?docs\.example\.com$ [NC]
RewriteRule ^(.*)/?$ http://www.example.com/$1 [R=301,NE,L]

Additional notes: With rules following are suggestions/improvements.

  • Additionally make sure you place your htaccess file at root level.
  • Use NC flag with host checking condition.
  • Use NE flag while redirecting.
  • Make sure your sub-domains are created by vhosts file.

Upvotes: 3

Related Questions