
Apache – Redirect from non www to www
If you are hosting a site, and you already setup your domain with and without www, but still having issues when accessing without www. A great way to solve it, is using the following configuration on your virtual host.
1 2 3 4 5 6 7 8 9 |
<VirtualHost *:80> ServerName example.com Redirect permanent / http://www.example.com/ </VirtualHost> <VirtualHost *:80> ServerName www.example.com # real server configuration </VirtualHost> |
If you are using SSL and https, you can use the following configuration:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<VirtualHost *:80> ServerName www.example.com ServerAlias example.com Redirect permanent / https://www.example.com/ </VirtualHost> <ifModule mod_ssl.c> <VirtualHost _default_:443> ServerName www.example.com ServerAlias example.com # real server configuration </VirtualHost> </IfModule> |