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

April 9, 2015 28.6k views
Security Ubuntu Apache Nginx

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

11 Answers

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.

What I would do:

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

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

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

Options +FollowSymlinks
RewriteEngine On

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

RewriteCond %{HTTP_HOST} ^www.your-site.com [NC]
RewriteRule ^(.*)$ http://your-site.com/$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 ~* www.yourdomain.com$) {
        return 301 http://yourdomain.com$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 (.*) 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]
  • Thanks for sharing your htaccess
    but my goal was to achieve this within NGINX config and not use htaccess at all

You already have the solution.

server {
       listen         80;
       server_name    yourdomain.com;
       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.

    Sorry.

  • ........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 (.*) https://www.pnrstatusbuzz.in/ %{REQUEST_URI}

Thank You.

  • My site name is finager.com
    What will be my code.
    Can you please write down it for me.
    I am newbee for write this sort of Code…

    TIA

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.

RewriteEngine On

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

RewriteCond %{HTTPHOST} ^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.

DjYogendra.Com

Have another answer? Share your knowledge.