Redirect non-www to www with Apache and NGINX

A two minute guide to redirecting all traffic on your website from the bare / naked / apex domain to www.

It is a common requirement for system and website administrators to redirect users to a common entry point for their website — also known as a canonical hostname. This is usually in one of two directions, i.e. www to non-www or non-www to www.

This brief guide shows techniques for achieving the latter and we demonstrate how to achieve this using two common web servers, Apache and NGINX.

By “non-www” we refer to the “bare domain”, also known as the “naked domain” or “apex domain”; however, the same technique can be used for redirecting other source domains (e.g. abc.example.com to xyz.example.com).

301 Moved Permanently

Before we start, what is actually happening under the covers.

The HyperText Transfer Protocol (HTTP) 301 Moved Permanently redirect status response code indicates that the resource requested has been definitively moved to the URL given by the Location headers. A browser redirects to this page and search engines update their links to the resource (in 'SEO-speak', it is said that the 'link-juice' is sent to the new URL).

— ref: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/301

In summary, when a user visits your website they will receive a 301 response code along with the new location of your page.

You can test any site out using the Redirect Checker site.

Apache HTTPD logo

Apache HTTPD

There are a few techniques available to administrators of Apache web servers, and we’ll touch upon two of them here. The most common technique used will be via a .htaccess file, as this is most accessible to a wider group (for example, because not everyone can administer their web server directly).

.htaccess

The .htaccess file can be placed in the root directory (e.g. /var/www/html) or can be in any sub-directory for more targeted configuration. Prefixed with a ‘dot’ it is a hidden file that is not served to users directly.

This technique requires the use of a module in Apache called mod_rewrite, which is widely enabled.

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,QSA,R=301]

Restart Apache HTTPD and you should now find that users are directed.

Virtual Host

If you have access to your web server configuration, you may choose to redirect users using a virtual host. This technique is shown below.

<VirtualHost *:443>
ServerName example.com
... # SSL Stuff
Redirect "/" "https://www.example.com/"
</VirtualHost>
<VirtualHost *:443>
ServerName www.example.com
... # SSL Stuff and other configuration
</VirtualHost>

Restart Apache HTTPD and you should now find that users are directed.

NGINX Logo

NGINX

Let’s assume your current NGINX configuration has an existing server block, along with an existing server_name that currently handles both your root domain (example.com) and its www variant (www.example.com).

server {
listen 443 ssl;
server_name example.com www.example.com;
... # ssl_certificate statements

To redirect we should include a new server block before the current one that handles hits to example.com and redirects them to www.example.com. We can also drop example.com from the server_name statement on the original block since it’s no longer required.

server {
listen 443 ssl;
server_name example.com;
... # ssl_certificate statements
return 301 https://www.example.com$request_uri;
}
server {
listen 443 ssl;
server_name www.example.com;
... # ssl_certificate statements

Restart NGINX and you should now find that users are directed.

A note from the author

Thank you for reading this article — I hope you found it useful to configure your site to use a canonical hostname.

You can follow me on Twitter and connect on LinkedIn.

DevOps | SRE | AWS | GCP https://twitter.com/davelms

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store