Question

How to redirect all traffic to https & non-www ?

I’m running Ubuntu 14.04 incl. SSL certificate via Serverpilot. I’m looking for a way to redirect all website traffic in the following order:

all http --> https all www --> non-www

Here’s what I have in my htaccess file:

RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule ^.*$ https://%1/$1 [R=301,L]

This works in Firefox but in Chrome & Safari (incl. iOS) it doesn’t redirect to https if I call my website via “non-ssl / non-www”.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

KFSys
Site Moderator
Site Moderator badge
January 29, 2024
Pinned Answer

An update on an older topic in case anyone stumbles upon it

To redirect non-www to www URLs using both Nginx or Apache, you can use the following configurations. First, we’ll provide the Nginx example, and then the Apache example using .htaccess rules.

Nginx Configuration:

  1. Open your Nginx site configuration file in a text editor. This file is usually located in /etc/nginx/sites-available/ or /etc/nginx/conf.d/ and typically has a .conf extension, like example.com.conf.

  2. Add the following server block configuration to redirect non-www to www:

server {
    listen 80;
    server_name example.com;
    return 301 $scheme://www.example.com$request_uri;
}

server {
    listen 80;
    server_name www.example.com;

    # Your existing Nginx configuration for www.example.com goes here
}

Replace example.com with your actual domain name. This configuration listens for requests to example.com and redirects them to www.example.com using a 301 (permanent) redirect.

  1. Save the file and exit the text editor.

  2. Test the Nginx configuration to ensure there are no syntax errors:

sudo nginx -t
  1. If the test is successful, reload Nginx to apply the changes:
sudo systemctl reload nginx

Apache Configuration using .htaccess:

  1. Create an .htaccess file in the root directory of your website if it doesn’t already exist.

  2. Add the following code to your .htaccess file to redirect non-www to www:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Replace `example.com` with your actual domain name.
  1. Save the .htaccess file.

  2. Test your Apache configuration by accessing your website in a web browser without the www prefix (e.g., http://example.com). It should automatically redirect to the www version (e.g., http://www.example.com).

Both the Nginx and Apache configurations provided above will redirect visitors from non-www URLs to www URLs. Make sure to adjust the domain name accordingly in the configuration examples.

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
April 13, 2015

To do that using nginx only, add the following server blocks:

server {
        server_name www.domain.com domain.com;
        listen 80;

        return 301 https://example.com$request_uri;
}

server {
        listen 443 spdy;

        ssl_certificate /path/to/domain.crt;
        ssl_certificate_key /path/to/domain.key;

        server_name www.domain.com;

        return 301 https://domain.com$request_uri;
}

Don’t forget to restart/reload nginx so that the changes take effect.

RewriteEngine On

RewriteCond %{HTTPS} off RewriteRule (.*) https://example.com%{REQUEST_URI} [L,R=301,NC]

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

this work absolutely fine and conditions are perfectely set for www to non www redirect with https

thanks. for this information. god bless u.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel