apache redirect from non www to www
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).
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]