17

I want to ensure that users come to www.mydomain.com even if they arrive through the alias mydomain.com. This is so that I have control over cookies on subdomains, and so that Google sees one single domain and not a hodge-podge of URLs.

How can I do this with apache?

hayalci
  • 3,611
  • 3
  • 25
  • 37
Magnar
  • 1,017
  • 2
  • 11
  • 15
  • 5
    Might I suggest you instead reverse this, and enforce mydomain.com instead? The 'www' prefix is redundant and entirely unnecessary. – Eevee Apr 30 '09 at 18:45
  • Cookies are sent to all subdomains on mydomain.com, while I can decide for myself what cookies are loaded from where with www.mydomain.com.. For instance, I can avoid adding cookies to static.mydomain.com, so that cookies aren't transmitted for every image that is loaded. It is definately not unnecessary. – Magnar May 01 '09 at 20:39
  • You can specifically tell Google to use a single linking style. See my answer below. – Robin Rodricks May 30 '09 at 06:55
  • I don't have the rep, but adding these tags would be helpful: redirect redirectmatch mod_alias – Tom Oct 13 '09 at 11:58
  • 1
    Even though this is _much_ later, it's still relevant. www is __not__ deprecated, because a domain with no subdomain has several disadvantages. See http://www.yes-www.org/why-use-www/ for several reasons to use www. – user50849 Oct 17 '14 at 20:13

5 Answers5

17
<VirtualHost ip:80>
   ServerName domain.com
   RedirectMatch permanent ^(.*)$ http://www.domain.com$1
</VirtualHost>

<VirtualHost ip:80>
   ServerName www.domain.com

   ... usual config
</VirtualHost>
Dave Cheney
  • 18,307
  • 7
  • 48
  • 56
  • 7
    For a slightly simpler solution, you can: "Redirect permanent / ptth://www.domain.com/" and apache will automatically preserve the suffix. (I reversed http to disable the autolink.) – Greg Hewgill Apr 30 '09 at 11:48
  • 1
    Really ? Sweet, I never knew that. I always thought you had to use RedirectMatch to preserve the trailing parts of the URI – Dave Cheney Apr 30 '09 at 15:08
4

Add this to your httpd.conf file:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
Magnar
  • 1,017
  • 2
  • 11
  • 15
4

Enable mod_rewrite support then create a .htaccess file in the root folder for you domain with the following content:

<IfModule mod_rewrite.c>
   Options +FollowSymLinks
   Options +Indexes
   RewriteEngine On
   RewriteBase /
   RewriteCond %{HTTP_HOST} ^domain\.com$
   RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
</IfModule>
Richard Slater
  • 3,228
  • 2
  • 28
  • 42
3

If you really want Google to use a specific domain style (with or without www) then create a free Google Webmaster Tools account, verify the ownership of your domain (upload a file) and set your preferred domain using the online control panel.

Its that simple, and you don't need to change any server side code, also inbound links with or without www will be treated the same increasing the PageRank of your pages.

Links may point to your site using both the www and non-www versions of the URL (for instance, http://www.example.com and http://example.com). The preferred domain is the version that you want used for your site in the search results.

Once you tell us your preferred domain name, we'll take your preference into account when displaying the URLs. It may take some time before you see this change fully reflected in our index.

If you don't specify a preferred domain, we may treat the www and non-www versions of the domain as separate references to separate pages.

Robin Rodricks
  • 540
  • 2
  • 10
  • 27
1

I had the opposite problem, people going to www.domain.com when I just want to serve directly from domain.com:

<VirtualHost *:80>
   ServerName domain.com

   ... usual config
</VirtualHost>

<VirtualHost *:80>
   ServerName www.domain.com
   ServerAlias sub.domain.com # can also catch some other subdomains
   ServerAlias *.domain.com # or all previously unmatched domains

   Redirect permanent / http://domain.com/
</VirtualHost>

As Greg Hewgill mentioned, using Redirect is a bit simpler than RedirectMatch and does the same job, see http://httpd.apache.org/docs/2.0/mod/mod_alias.html#redirect for more details.

Tom
  • 2,622
  • 1
  • 20
  • 12