Report this

What is the reason for this report?

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

Posted on April 9, 2015

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



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!

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

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.

I use .htaccess

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 comment has been deleted

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.