apache mod-rewrite redirect

apache redirect from non www to www


I have a website that doesn’t seem to redirect from non-www to www.

My Apache configuration is as follows:

RewriteEngine On
### re-direct to www
RewriteCond %{http_host} !^ [nc]
RewriteRule ^(.*)$$1 [r=301,nc] 

What am I missing?



Using the rewrite engine is a pretty heavyweight way to solve this problem. Here is a simpler solution:

<VirtualHost *:80>
    Redirect permanent /

<VirtualHost *:80>
    # real server configuration

And then you’ll have another <VirtualHost> section with ServerName for your real server configuration. Apache automatically preserves anything after the / when using the Redirect directive, which is a common misconception about why this method won’t work (when in fact it does).


  • 25

    How do you do it for a site that has an ssl virtual host as well?

    Dec 11, 2010 at 16:16

  • 29

    I get the error “The webpage at has resulted in too many redirects” when using this suggestion. Do others have this problem?

    Mar 28, 2011 at 19:38

  • 4

    @BlackDivine: There’s nothing magical about doing it in the other direction. Simply swap www.example and example wherever they appear in the sample.

    Feb 12, 2012 at 21:17

  • 11

    @JonathanBerger If you too many redirects, then you have probably not configured well the file. Make sure to have 2 VirtualHosts: one with non-www which is the above and the other with ServerName which has the real configuration. Also make sure to have not a redirect in configuration as well (both mod_alias and mod_rewrite).

    Aug 4, 2013 at 8:03

  • 3

    For a virtual host server, say, I have Redirect 301 / but when it redirects, it misses the trailing slash, meaning that goes to Any ideas? (Apache 2.2.22)

    Jan 8, 2014 at 16:45

112 =>

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


  • 4

    Oh that’s neat, this can be included in the server config at the top level (and if so) will apply to every virtual host!

    Aug 19, 2014 at 23:02

  • Hello @burzumko how do you achieve this, which is true for all without www, but then let, say* go through without www ?

    Mar 18, 2015 at 13:08

  • 2

    I used this solution for HTTPS virtual hosts too. Just add an s to http in the third row.

    – rodrigoap

    Sep 12, 2017 at 18:10


<VirtualHost *:80>
    RedirectMatch permanent ^/(.*)$1


  • This didn’t work for me; it caused an infinite redirect loop to the same site

    May 3, 2014 at 6:09

  • 2

    @dmiller309: Do you happen to have included www. in the ServerAlias?

    – cherouvim

    May 3, 2014 at 13:28

  • 2

    You’re right, I accidentally put www. in the ServerAlias using the *. wildcard. Because I messed up the ordering of the VirtualHost entries, the *. wildcard had the opportunity to match when I didn’t think it would.

    May 10, 2014 at 21:28

  • 8

    This is the best answer, because it’s clean, keeps the path (unlike answer #1) and doesn’t use Rewrite.

    Jan 31, 2015 at 9:35