.htaccesshttp-redirectsubdomainno-www

Remove www from subdomain and then redirect to subdirectory


I'm trying to add some rules to an .htaccess file in a subdomain to:

  1. Remove the www from the subdomain (if present) (E.g.: www.subdomain.domain.com to subdomain.domain.com)
  2. Redirect to the /beta subdirectory (E.g: subdomain.domain.com to subdomain.domain.com/beta)

I have the second part only:

# Redirect to /beta
RewriteEngine On
RewriteRule ^$ /beta [L]

I've seen a couple of examples here in StackOverflow but they seem to work only for domains, not subdomains. Any help would be appreciated.


Solution

  • To redirect old domain to new domain this should do:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www.subdomain.domain.com$ [NC]
    RewriteRule ^(.*)$ http://subdomain.domain.com/$1 [R=301]
    

    Note that I omitted the L directive here ([R=301,L]) because you are probably going to add the second directive (/beta redirect) right after this.