• 301 redirect all subdomains to new domain on apache

    While trying to consolidate many subdomains under one new domain on apache we had to add the following into the sites configuration file to redirect the multiple subdomains to one. This could be added into the .htaccess as well.

    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} !^newdomain.com$ [NC]
    RewriteRule ^(.*)$ https://newdomain.com/$1 [L,R=301]

    Instead of writing multiple conditions to catch each individual subdomain request, this will catch all of the subdomains and 301 redirect them to the new domain. This will also keep any links you may wish to preserve.

    For example:
    olddomain.com/blog will redirect to newdomain.com/blog
    www.oldomain.com/file will redirect to newdomain.com/file

    If you wish to have all olddomain.com pages just redirect to newdomain.com’s homepage and don’t care to preserve anything remove the $1 from the above code.

    It is important to note that you need to maintain the olddomain.com and all old subdomains in the ServerAlias part of the apache config or else this will not work at all. If you remove the olddomain.com the apache server obviously will not respond to any requests for olddomain.com or any subdomains.


    1 responses to “301 redirect all subdomains to new domain on apache” RSS icon

    Leave a reply