How can I enforce that my urls always start with www?
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?
Solution 1:
<VirtualHost ip:80>
ServerName domain.com
RedirectMatch permanent ^(.*)$ http://www.domain.com$1
</VirtualHost>
<VirtualHost ip:80>
ServerName www.domain.com
... usual config
</VirtualHost>
Solution 2:
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]
Solution 3:
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>
Solution 4:
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.