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

Posted April 9, 2015 36.1k views

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

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.

Submit an Answer
11 answers

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

server {
        listen 80;

        return 301$request_uri;

server {
        listen 443 spdy;

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


        return 301$request_uri;

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

What I would do:

edit /etc/apache2/sites-enabled/000-default.conf, and replace the contents with this (changing to your sites URL):

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        Redirect permanent /
        ErrorLog ${APACHE_LOG_DIR}/error.log
        LogLevel warn
        CustomLog ${APACHE_LOG_DIR}/access.log combined

Next, edit your .htaccess, and replace what you have in your message with this (again, changing to your URL):

Options +FollowSymlinks
RewriteEngine On

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} 

RewriteCond %{HTTP_HOST} ^ [NC]
RewriteRule ^(.*)$$1 [L,R=301]
  • Thank you JonsJava for your effort but since it’s serverpilot (nginx/apache mix) it’s not that easy.

    all www –> non-www
    I achieved non-www by adding a new config file to my “app” folder with this content:

    if ($host ~*$) {
        return 301$request_uri;

    all http –> https
    Unfortunately I didn’t manage to force SSL by tweaking system files.
    So I had to add this to my htaccess file:

    RewriteCond %{ENV:HTTPS} !on
    RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]

    The htaccess entry is also the solution serverpilot recommended. I don’t like it at all.
    So if anyone knows how to pull this off within the system config I’d be very thankful.

I use .htaccess

RewriteEngine On 

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

RewriteCond %{HTTP_HOST} ^ [NC]
RewriteRule (.*){REQUEST_URI} [L,R=301,NC]

You already have the solution.

server {
       listen         80;
       return         301 https://$server_name$request_uri;

I literally just took your www-to-non-www and converted it to http to https.

  • ……………And I just got around to reading all the comments where you already got the answer.


  • ........And I just got around to reading all the comments where you already got the answer.

    No need to be sorry, I appreciate you taking the time to help me out :)
    So thank you very much!

I intended to redirect non-www to www and http to https. For that I have used below code and it’s resolving perfectly. Please check whether code is correct or not.

RewriteEngine On
RewriteBase /
#Redirect non-www to www

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

# Redirect to HTTPS
RewriteCond %{HTTPS} off
RewriteRule (.*) %{REQUEST_URI}

Thank You.

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

I also need all requests to resolve to https.

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

Previous 1 2 Next