• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
LLODO – News – Tech and Education

LLODO - News - Tech and Education

Breaking News, Latest News about education, Technoly

  • News Tech
  • World News
  • Education
  • Science
You are here: Home / Technology / Redirect HTTP to HTTPS using Nginx or Apache2 on Ubuntu 17.04

Redirect HTTP to HTTPS using Nginx or Apache2 on Ubuntu 17.04

05/21/2019 by admin

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;
  server_name yourdomain.com www.yourdomain.com;

  # redirect http to https with www
  return 301 https://www.example.com$request_uri;
}

server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  server_name yourdomain.com;
  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 https://example.com$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.

Summary:

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.

Enjoy!

You may also like the post below:

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

Related posts:

  1. Students Tutorial – Allow WordPress Permalinks on Nginx or Apache2
  2. Apache2 301 Redirect Without htaccess File on Ubuntu 17.04
  3. Setup Varnish and Apache2 on Ubuntu 17.04 / 17.10
  4. Setup Apache2 Virtual Hosting on Ubuntu 17.04 / 17.10
  5. Install WordPress on Ubuntu 17.04 / 17.10 with Apache2 HTTP/2 and Let’s Encrypt SSL
  6. Running Multiple WordPress Sites on Ubuntu 16.04 LTS with Apache2, MariaDB and PHP 7.1 Support
  7. Install Ghost CMS on Ubuntu 16.04 / 17.10 / 18.04 with MariaDB and Apache2 Proxy
  8. Install the Latest Apache2 HTTP Server ( 2.4.34 ) on Ubuntu 16.04 / 17.10 / 18.04 LTS Servers
  9. Apache2 HTTP Server ( 2.4.35 ) Released — Here’s How to Install / Upgrade on Ubuntu 16.04 / 18.04 LTS

Filed Under: Technology Tagged With: Apache2

Primary Sidebar

Featured Posts

[LLODO] ‘A Touch of Disney’: Disneyland announces dates, details of ticketed food experience

02/25/2021

[LLODO] Navy vet died after California police knelt on his neck for nearly 5 minutes, family alleges

02/25/2021

[LLODO] California candidate for governor accuses Newsom of creating ‘education equality gap’ during COVID

02/25/2021

[LLODO] Pennsylvania teen, 15, charged after stabbing his siblings, ages 13 and 9, prosecutors say

02/25/2021

[LLODO] Capitol police union slams ‘dysfunction’ at riot hearing as cops suffer crushed spinal discs, brain injuries

02/25/2021

Categories

  • Applications (750)
  • Business (6,322)
  • Education (2,405)
  • Health (4,182)
  • Iphone (2,005)
  • News Tech (11,647)
  • Reviews (6,041)
  • Science (1,429)
  • Technology (13,615)
  • World News (286)

Home - About Us - Privacy Policy - Contact Us - Site map
Copyright © 2021 · LLODO.COM - Internet Do - QA Do Eng