Categories
apache mod-rewrite redirect

apache redirect from non www to www

229

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} !^www.example.com [nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc] 

What am I missing?

3

550

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

<VirtualHost *:80>
    ServerName example.com
    Redirect permanent / http://www.example.com/
</VirtualHost>

<VirtualHost *:80>
    ServerName www.example.com
    # real server configuration
</VirtualHost>

And then you’ll have another <VirtualHost> section with ServerName www.example.com 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).

28

  • 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 example.com 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 www.example.com which has the real configuration. Also make sure to have not a redirect in www.example.com configuration as well (both mod_alias and mod_rewrite).

    Aug 4, 2013 at 8:03

  • 3

    For a virtual host server, say example.com, I have Redirect 301 / http://example2.com/extra/ but when it redirects, it misses the trailing slash, meaning that example.com/blah goes to example2.com/extrablah. Any ideas? (Apache 2.2.22)

    Jan 8, 2014 at 16:45


112

http://example.com/subdir/?lold=13666 => http://www.example.com/subdir/?lold=13666

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

3

  • 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 example.com/subdi1/ws/* 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

56

<VirtualHost *:80>
    ServerAlias example.com
    RedirectMatch permanent ^/(.*) http://www.example.com/$1
</VirtualHost>

4

  • 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