Check this Out!Visit our social network pages for recent updates!

Redirect HTTP to HTTPS using Nginx or Apache2 on Ubuntu 17.04

For students and new users using Ubuntu 17.04 with Nginx or Apache2 webserver to power a WordPress blog or website, here’s a brief tutorial that shows you how to easily redirect all HTTP traffic to HTTPS.

By now you probably already know that running your blog pages over HTTPS is recommended by all major search engines. In fact, if you want your web pages to rank better on Google search engine result pages, you may need to run them over HTTPS.

This post isn’t going to show you how to implement HTTPS for your blogs. It shows you how to redirect all HTTP traffic to HTTPS after HTTPS has been enabled on Nginx or Apache2 web server.

Assuming you already have HTTPS working on your pages, continue below to configure proper redirects for your sites.

Nginx HTTP to HTTPS Redirects

Now that HTTPS is working, below is a highlighted server block to add to your existing Nginx configuration page. The block redirects all HTTP traffic to HTTPS with WWW preceding the domain name.

server {
  listen 80;
  listen [::]:80;

  # redirect http to https with www
  return 301$request_uri;

server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  root /var/www/html;

  # SSL code

## PHP settings
    location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    try_files $uri =404;
    fastcgi_index index.php;

Add the top highlighted server block and save the file.

If you want to redirect all HTTP traffic to HTTPS without the WWW replace the highlighted redirect line above with the one below.

# redirect http to https without www
  return 301$request_uri;

This is how you do it using Nginx web server.

Apache2 HTTP to HTTPS Redirects

For Apache2 web servers, you must enable mod_rewrite module. To do that, run the commands below

sudo a2enmod rewrite

After enabling the module above, add the code below to your .httaccess  file.. normally in the root directory of your site to redirect all HTTP traffic to HTTPS.

RewriteEngine On
# redirect http to https
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

If you want to redirect all HTTP to HTTPS with WWW preceding, replace the code above with the one below.

RewriteEngine On
# redirect all non-www to https with www
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%1/$1 [L,R=301]
# redirect http www to https www
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

If you want redirect all traffic to HTTPS without WWW, then use the code below

RewriteEngine On
# redirect all www to https non-www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [L,R=301]
# redirect http non-www to https non-www
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Restart the web server and you’re done.


This is a post that shows you how to do a proper HTTP to HTTPS redirect when using Apache2 or Nginx webserver on Ubuntu or other Linux distributions. Implement HTTPS for your sites will be written in a different post.

Please stay tuned for future posts on implementing HTTPS for your websites and blogs.


You may also like the post below:

Lesson 39:Configure HTTP Strict Transport Security (HSTS) on Apache2

Leave a Reply