Combining Apache ServerAlias and 301 Redirects

301-redirectapache-2.4serveralias

I've referenced this question but I'm not sure if I'm clear on how this works given this situation:

I have a client with two brands that are currently online like this:

Brand A (site 1)    Brand B (site 2)
---                 ---  
site 1 pages        site 2 pages

Now, they've had a new site built that combines content from both sites and unifies their two brands into one:

Brand Unified (site 3)
---
site 3 pages

Clearly there are 301 redirects I want to put into place for each original site after I update the A records for each domain. However, the new site is a custom WordPress theme and WP only allows for one domain per installation. This leaves me with a situation where I can assign the original TLD from, say, Brand A as the site URL in WordPress, and then have the Brand B domain redirect to A.

I thought I would use a server block like this in the Apache site conf:

<VirtualHost *:80>
    ServerName site1.com  #site1.com now points directly here
    ServerAlias site2.com #site2.com points directly but redirects
    ServerAdmin admin@brandAsite.com
    DocumentRoot /var/www/newsite

So, assuming there is nothing wrong with this configuration, what happens if I try to implement 301 redirects for site1 and site2 links? Specifically, since site2.com is now listed as an alias of site1.com, can I still effectively have 301 redirects in the .htaccess file in the /var/www/newsite/ directory or does ServerAlias interfere with that? I'm wondering if it gets aliased to back to site1.com and therefore the 301 rules wouldn't trigger?

Best Answer

If you are able to configure Apache conf, why complicate situation and put redirects (or anything) in .htaccess files? You seem to assign site2.com to /var/www/newsite only for the purpose to using .htaccess and nothing else. I would keep it simple, everything in one place:

NameVirtualHost  *:80
<VirtualHost *:80>
    ServerName site1.com
    DocumentRoot /var/www/newsite
</VirtualHost>
<VirtualHost *:80>
    ServerName site2.com

    # Specifically mapped redirects
    Redirect permanent /foo/ http://site1.com/new-foo/

    # Root redirect (if no other cases above match)
    Redirect permanent /  http://site1.com/
</VirtualHost>
Related Topic