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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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.

I cannot for the life of me get all non-www requests on my WordPress site to redirect to www. Eg. example.com/contact does not resolve to www.

I also need all requests to resolve to https.

I’ve tried the solutions posted above with no luck.