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?

5
  • 5
    Might I suggest you instead reverse this, and enforce mydomain.com instead? The 'www' prefix is redundant and entirely unnecessary.
    – Eevee
    Apr 30, 2009 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 1, 2009 at 20:39
  • You can specifically tell Google to use a single linking style. See my answer below. May 30, 2009 at 6:55
  • I don't have the rep, but adding these tags would be helpful: redirect redirectmatch mod_alias
    – Tom
    Oct 13, 2009 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 yes-www.org/why-use-www for several reasons to use www.
    – user50849
    Oct 17, 2014 at 20:13

5 Answers 5

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>
2
  • 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.) Apr 30, 2009 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 Apr 30, 2009 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]
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>
3
  • don't forget to make the RewriteCond test case-insensitive...
    – Alnitak
    Apr 30, 2009 at 12:30
  • Only do this if you don't have access to make changes to the apache config file for some reason. .htaccess files have a lot of overhead, and rewrites inside them make it much worse. May 4, 2009 at 4:07
  • using Redirect is always preferable to using mod_rewrite for this task. Jun 3, 2009 at 12:05
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.

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .