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.

David Sugden
3 min readMar 21, 2021

--

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).

--

--