106

This will change domain.com to www.domain.com:

# Force the "www."
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

How do I replace the "domain" part so that this works on any domain?

StackOverflowNewbie
  • 35,023
  • 98
  • 252
  • 421

8 Answers8

256

I would use this rule:

RewriteEngine On
RewriteCond %{HTTP_HOST} !=""
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

The first condition checks whether the Host value is not empty (in case of HTTP/1.0); the second checks whether the the Host value does not begin with www.; the third checks for HTTPS (%{HTTPS} is either on or off, so %{HTTPS}s is either ons or offs and in case of ons the s is matched). The substitution part of RewriteRule then just merges the information parts to a full URL.

Max Vollmer
  • 8,102
  • 9
  • 27
  • 43
Gumbo
  • 594,236
  • 102
  • 740
  • 814
  • 33
    This solution works only if you want all sub-domains forwarded to www.yourdomain.com. If you only want to force www, you should update the second line to be: `RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]` – Jared Pomranky May 14 '13 at 19:04
  • 2
    I don't get _any_ of my subdomains forwarded to www.mydomain.com using the rule posted in the question... – lol Jul 18 '13 at 23:21
  • 3
    very clever! I like you forced the HTTPS check to return an "S" in the capture group, which you used on the following line. very elegant – code_monk Feb 22 '14 at 21:31
  • Seems Like a Universal answer , covers HTTPs and normal protocols, works like a charm. – Clain Dsilva Mar 03 '15 at 05:27
  • 1
    Thanks! I would add `RewriteEngine On` as the first line to complete it, as this could lead to a 500 Server Error if copy-pasted as it is. – Ryan Casas Oct 07 '15 at 16:50
  • @BaldEagle That edit should never have been approved. It didn't improve the answer (solving that issue would have), instead it should've been a comment. Also it's actually not an issue. This works perfectly fine for such URLs. – Max Vollmer Nov 24 '18 at 12:54
46

This will do it:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Wade
  • 3,509
  • 2
  • 26
  • 45
Martin Drapeau
  • 1,474
  • 14
  • 15
4

If you want to redirect all non-www requests to your site to the www version, all you need to do is add the following code to your .htaccess file:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
ClickForWebs
  • 57
  • 1
  • 3
3

This won't work with subdomains.

domain.com correctly gets redirected to www.domain.com

but

images.domain.com gets redirected to www.images.domain.com

Instead of checking if the subdomain is "not www", check if there are two dots:

RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteCond %{HTTP_HOST} !^(.*)\.(.*)\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ HTTP%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
  • I like this solution. Anyway to accomplish the same thing with TLDs like .co.uk? – TylersSN Mar 12 '15 at 13:57
  • 1
    @iUseMagentoNow just increase the dots, for example: `RewriteCond %{HTTP_HOST} !^(.*)\.(.*)\.(.*)\. [NC]` (Strictly speaking, your TLD is still `.uk` and your domain is `something.co`.) – mirabilos Dec 15 '15 at 14:17
  • .co in this case is a second level domain and is not actually a part of your domain where as .uk is a country code TLD. To be honest, having worked in the hosting industry and seeing the new formats for domain names i cant see the current TLD formats lasting for anything other than official uses. goverments, organisations etc. In the near future when you can register fqdn like, fred.bloggs for a similar price to current norms then the personal web will look very different from a DNS point of view. – Chris Feb 04 '16 at 21:43
1
RewriteEngine On

RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

This redirects example.com to www.example.com excluding subdomains.

Albert Renshaw
  • 15,644
  • 17
  • 92
  • 173
Amit Verma
  • 38,175
  • 19
  • 80
  • 104
  • For me this seems to be sending www.example.com to www.www.example.com haha – Albert Renshaw Jan 25 '17 at 05:41
  • @Albert Perhaps this is because of your browser cache. Clear your cache and Retry. – Amit Verma Jan 25 '17 at 06:04
  • You are correct! I just ran in incognito (chrome) and it worked fine, thanks! – Albert Renshaw Jan 25 '17 at 06:37
  • 1
    I like this generic rule very much. But it doesn`t work for http://domain.co.uk as it already has two dots in it. How could this rule be changed to make it work for .co.uk as well? – Sacha Vorbeck Jul 03 '17 at 09:13
  • 1
    For .co.uk and other sub cctlds this works: ^([^.]+\.[^.]+)(\.(uk|ar|cy|ar|tr))?$ but unfortunately there are some mixed typed tlds (https://wiki.mozilla.org/TLD_List) like es that allow domain.es as well as domain.com.es. Adding |es to the regex would match domain.es as well as www.domain.es which is not wanted here. Have to find a way to handle this mixed typed sub-cctlds – Sacha Vorbeck Jul 03 '17 at 16:36
1

The following should prefix 'www' to any request that doesn't have one, and redirect the edited request to the new URI.

RewriteCond "%{HTTP_HOST}" "!^www\."         [NC]
RewriteCond "%{HTTP_HOST}" "(.*)"
RewriteRule "(.*)"         "http://www.%1$1" [R=301,L]
RoUS
  • 1,634
  • 2
  • 12
  • 27
0

This is an older question, and there are many different ways to do this. The most complete answer, IMHO, is found here: https://gist.github.com/vielhuber/f2c6bdd1ed9024023fe4 . (Pasting and formatting the code here didn't work for me)

Rick Hellewell
  • 800
  • 6
  • 23
-1

this worked like magic for me

RewriteCond %{HTTP_HOST} ^sitename.com [NC] RewriteRule ^(.*)$ https://www.sitename.com/$1 [L,R=301,NC]

KofiYah
  • 2,021
  • 1
  • 12
  • 13