Apache Virtual Host Config www vs non-www, Rewrite or sServerAlias?

19,984

Solution 1

I would probably do both. With something like:

<VirtualHost 111.22.33.44:80>
  ServerName subdomain.site.com

  #Using Wildcard: might as well handle any variation
  #such as ww.subdomain.site.com (remember to set this in DNS too)
  ServerAlias *.subdomain.site.com

  RewriteEngine On

  #Change all variations to the Canonical hostname for SEO.
  RewriteCond %{HTTP_HOST} !^subdomain.site.com [NC]
  RewriteRule ^/(.*)$ http://subdomain.site.com/$1 [R=301]
  Include conf/subdomain.conf 
</VirtualHost>

Note: the solution by David Zaslavsky above does more or less the same thing, but this way you don't have to do a separate VirtualHost section for each subdomain.

Solution 2

I'd imagine the rewriting solution gets better SEO-foo (nice term :-P) since it's usually considered best to have one canonical domain that everybody gets sent to for a particular set of content. In other words, having two different domains that produce the same results from the server can split your site rankings between the two domains, reducing the value of each. (Google allows you to specify a canonical domain using its Webmaster Tools, but that only works on Google)

I think you can actually use a Redirect here, i.e.

<VirtualHost *:80>
    ServerName www.subdomain.site.com
    Redirect permanent / http://subdomain.site.com/
</VirtualHost

That is less computationally intensive than invoking mod_rewrite.

Solution 3

Here's what works for me:

<VirtualHost *:80>

ServerName www.domain.com
ServerAlias domain.com

# [ snip some unrelated stuff ]

# Redirect secondary hostnames to canonical hostname
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule (.*) http://www.domain.com$1 [R=301,L]

</VirtualHost>

The advantage of doing this instead of RedirectMatch is that it will redirect domain.com/about-us to www.domain.com/about-us instead of just redirecting every request to the home page. It also uses a 301 redirect, which transfers search engine ranking from domain.com/about-us to www.domain.com/about-us.

Share:
19,984

Related videos on Youtube

Jack ilkgu
Author by

Jack ilkgu

#SOreadytohelp

Updated on September 17, 2022

Comments

  • Jack ilkgu
    Jack ilkgu over 1 year

    We have a central httpd.conf and we include confs for various virtual hosts. Until today, we didn't really have a need for "www.subdomain.site.com" domains, only "subdomain.site.com." Now we do, so I am trying to figure out which of these two approaches is better:

    [1]. Use a Rewrite:

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

    OR

    [2]. Use ServerAlais:

        <VirtualHost 111.22.33.44:80>
        ServerName subdomain.site.com
        ServerAlias www.subdomain.site.com
        Include conf/subdomain.conf
        </VirtualHost>
    

    I imagine there is more processing on the Rewrite, but unsure of how ServerAlias would behave in this mix. Does one get better SEO foo than the other?

    Any and all ideas welcome!

    Thanks,

    KM

    • David Z
      David Z almost 14 years
      In the rewrite, the RewriteCond needs to go first, and the variable should be %{HTTP_HOST}.
    • user649102
      user649102 over 11 years
      @DavidZaslavsky I fixed that
  • Jack ilkgu
    Jack ilkgu almost 14 years
    Dave and Matthew, thank you both for your suggestions. Between the two suggestions I have more than enough to get going in the right direction!
  • armadadrive
    armadadrive over 7 years
    Does this directive work only for the root domain or does it redirect all specific endpoints following the / as well?
  • David Z
    David Z over 7 years
    According to the Redirect documentation, it redirects everything beginning with the specified path, not only the root.