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)
ref: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/301301 Moved Permanently
redirect status response code indicates that the resource requested has been definitively moved to the URL given by theLocation
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).
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.
7 responses to “Redirect non-www to www with Apache and NGINX”
When someone writes an piece of writing he/she retains the plan of a user in his/her brain that how a user can be aware of it.
Therefore that’s why this article is perfect.
Thanks!
It’s not my first time to pay a visit this site, i am browsing this site dailly
and obtain pleasant facts from here everyday.
Awesome! Its really awesome article, I have got much clear
idea concerning from this piece of writing.
Your article is superb. We loved reading it. Thanks for posting such a great post.
Thank you for the auspicious writeup. It in fact was a amusement
account it. Look advanced to more added agreeable from
you! By the way, how could we communicate?
It’s very straightforward to find out any topic on web as
compared to books, as I found this article at this website.
My partner and I stumbled over here by a different website
and thought I might check things out. I like what I see so i am just following you.
Look forward to looking at your web page repeatedly.