Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Web Servers»Nginx»Nginx – The Best Way to Force Redirect to HTTPS with WWW URL

    Nginx – The Best Way to Force Redirect to HTTPS with WWW URL

    RahulBy RahulOctober 2, 20171 Min ReadUpdated:October 10, 2019

    Questions- How do I force redirect users to https with www prefix domain. How do I redirect a user to https in Nginx? The Best Way to Force Redirect Users to WWW and HTTPS.

    This tutorial will provide you the best way to redirect users forcefully to https:// URL with www domain prefix. For example, your domain name is example.com, Below configuration will forcefully redirect users to https://www.example.com in all ways.

    Let’s edit Nginx virtual host configuration file:

    sudo vim /etc/nginx/sites-enabled/example.com.conf
    

    and add the below content. change example.com with your actual domain name. Also, add any other required settings specific for your Virtual host.

    # Force all users to https://www.example.com
    
    server {
        listen 80;
        server_name example.com www.example.com;
        return 301 https://www.example.com$request_uri;
    }
    server {
        listen 443 ssl;
        server_name example.com;
    
        ssl_certificate /etc/nginx/ssl/www.example.com.pem;
        ssl_certificate_key /etc/nginx/ssl/www.example.com.key;
        return 301 https://www.example.com$request_uri;
    }
    
    server {
        listen 443 ssl;
        server_name www.example.com;
        root /var/www/html
    	
        error_page  403 /error/404.html;
        error_page  404 /error/404.html;
        error_page  500 502 503 504 /error/50x.html;
    
        ssl_certificate /etc/nginx/ssl/www.example.com.pem;
        ssl_certificate_key /etc/nginx/ssl/www.example.com.key;
    }
    

    After making the change to the configuration file, restart your Nginx web server and try to access your domain like URLs. In all the conditions, this will be redirected to https://www.example.com.

    https nginx redirect
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Install MariaDB 10.4 on CentOS/RHEL 7/6
    Next Article How to Install PostgreSQL on CentOS/RHEL 7

    Related Posts

    How To Install Linux, Nginx, MySQL, & PHP (LEMP Stack) on Ubuntu 22.04

    Updated:April 7, 20227 Mins Read

    How to Increase Request Timeout in NGINX

    Updated:January 13, 20222 Mins Read

    How to Install Nginx, MySQL & PHP (LEMP) on Ubuntu 20.04

    5 Mins Read

    How To Install Nginx with PHP-FPM on Ubuntu 20.04

    Updated:December 3, 20203 Mins Read

    How to Install Nginx on Ubuntu 20.04

    3 Mins Read

    How to Configure Nginx for WordPress Permalinks

    Updated:April 21, 20202 Mins Read

    8 Comments

    1. dwarkanath on October 9, 2019 9:43 am

      Hello in which file ur given code need to write

      Reply
    2. cooper on October 7, 2019 12:04 pm

      web in hml get error ngnix don’t restart, and if i install force_https templates too get error ngnix don’t restart

      this is an ngnix error?

      thanks

      Reply
    3. Vinchenzo on August 10, 2019 11:44 pm

      Tnx for the help!

      Reply
    4. Rajesh Chaudhary on June 12, 2019 11:00 am

      Thank You.

      Reply
    5. linki on April 22, 2019 2:46 pm

      Thanks for sharing all of your experiences!

      Reply
    6. Rafael on January 18, 2019 2:43 am

      Hello, what is the path of the file, Nginx virtual host, to edit and insert the information reported in your tutorial?
      Thank you very much.

      Reply
    7. Kirk Douglas on January 10, 2019 12:32 am

      Thank you very much. This was exactly what I was looking for.

      Reply
    8. ibnumalik on July 17, 2018 8:03 am

      thanks for the snippet

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • How To Install Docker on Ubuntu 22.04
    • How to Install Bower on Ubuntu 22.04 & 20.04
    • How to run “npm start” through Docker
    • Filesystem Hierarchy Structure (FHS) in Linux
    • How to accept user input in Python
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.