BRAHMA TECHNOLAB

Redirect non-www to www with Apache and NGINX

i.e.

  • www to non-www  
  • 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

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.

 

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

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.

LET’S DISCUSS YOUR PROJECT

7 responses to “Redirect non-www to www with Apache and NGINX”

Leave a Reply

Your email address will not be published. Required fields are marked *

Snehal

Typically replies within a day

Hello, Welcome to BRAHMA TECHNOLAB. Please click below button for chatting us through Skype.