Apache config for redirect from www to just domain

Blog

You can use a VirtualHost directive to redirect from www.example.com to example.com at the Apache level

# redirect www to no-host
<VirtualHost *:80>
  ServerName www.example.io
  Redirect permanent / http://example.io
</VirtualHost>

# redirect SSL www to no-host
<VirtualHost *:443>
  ServerName www.example.io
  Redirect permanent / https://example.io
</VirtualHost>

That second one will likely fail unless you have the SNI (Server Name Indication) TLS extension on your server.

Then, include the rest of your Apache configuration as normal:

<VirtualHost *:80>
  ServerName example.io
  ServerAdmin hello@example.io
  DocumentRoot /var/www/example.io/html/
  ErrorDocument 404 /404.html
  ErrorDocument 403 /403.html
  ...
</VirtualHost>
...

Add new comment