Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
4  
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 1 '09 at 20:39
    
You can specifically tell Google to use a single linking style. See my answer below. –  Jarvis May 30 '09 at 6: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
    
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 at 20:13

5 Answers 5

up vote 16 down vote accepted
<VirtualHost ip:80>
   ServerName domain.com
   RedirectMatch permanent ^(.*)$ http://www.domain.com$1
</VirtualHost>

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

   ... usual config
</VirtualHost>
share|improve this answer
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

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]
share|improve this answer

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>
share|improve this answer
    
don't forget to make the RewriteCond test case-insensitive... –  Alnitak Apr 30 '09 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. –  John Dalton May 4 '09 at 4:07
    
using Redirect is always preferable to using mod_rewrite for this task. –  David Pashley Jun 3 '09 at 12:05

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.

share|improve this answer

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%5Falias.html#redirect for more details.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.