Reputation: 40633
I want to force "www." on my URLs (e.g. http://domain.com becomes http://www.domain.com). However, I don't want it forced on URLs that already have a subdomain (e.g. http://images.domain.com should NOT become http://www.images.domain.com). The following snippet I found on the net does the latter:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
What do I need to do to get this to work for me? Thanks.
Upvotes: 5
Views: 3403
Reputation: 7494
Simple The rules below force www, except for subdomains.
RewriteCond %{HTTP_HOST} =name.domain [NC]
RewriteRule ^(.*)$ http://www.name.domain/$1 [R=301,L]
Edit and paste it, restart apache.
Explain:
RewriteCond %{HTTP_HOST} =name.domain [NC]
match only when someone types name.domain (your domain name).
When types subdomain.name.domain the RewriteCond is false and not redirect. You understand? In rule that you previously posted you was matching for !(not)^(beginning by)www and subdomain.name.domain satisfied RewriteCond and is what you don't want. :)
Upvotes: 6