Cloudbooklet
  • News
  • Artificial Intelligence
  • AI Tools
  • Applications
  • Linux
No Result
View All Result
Cloudbooklet
  • News
  • Artificial Intelligence
  • AI Tools
  • Applications
  • Linux
No Result
View All Result
Cloudbooklet
No Result
View All Result
Home Google Cloud

Nginx Redirect URLs HTTP to HTTPS

by Cloudbooklet
4 years ago
in Google Cloud
Nginx Redirect Urls Http To Https
ShareTweetSendShare
ADVERTISEMENT

Nginx Redirect URLs HTTP to HTTPS with www or non-www in your URL. Redirecting a URL is necessary in Nginx to inform the visitor that the page they are requesting resides in another location. This also helps in SEO by preventing the duplication of the URL.

For example a domain name with the prefix www and non-www will be considered as different domains in the view of search engines, so it is necessary to redirect all non-www urls with www and vice versa.

In this guide we are going to learn how to redirect HTTP to HTTPS and also redirect non-www to www.

ADVERTISEMENT

Redirect with www in Nginx

To redirect a url with www you can add the below code inside your server block.

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

    server_name domain.com www.domain.com;

    return 301 http://www.domain.com$request_uri;
}

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

    server_name www.domain.com; 

    #your configurations here
} 

The above configuration catches all HTTP requests for the domain.com and www.domain.com and redirects it to www.domain.com

Redirect to non-www in Nginx

To redirect a url with www you can add the below code inside your server block.

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

    server_name domain.com www.domain.com;

    return 301 http://domain.com$request_uri;
}

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

    server_name domain.com; 

    #your configurations here
} 

The above configuration catches all HTTP requests for the domain.com and www.domain.com and redirects it to domain.com without www.

Redirect to HTTPS with www

To redirect a url with HTTPS you need to install Let’s Encrypt SSL and you can add the below code inside your server block.

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

    server_name domain.com www.domain.com;

    return 301 https://www.domain.com$request_uri;
}

server {
    listen [::]:443 ssl http2;
    listen 443 ssl http2;

    server_name domain.com;

    ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem;
 
    root /var/www/html/domain.com/public/;
    index index.php index.html;

    return 301 https://www.domain.com$request_uri;
} 

 server {
    listen [::]:443 ssl http2;
    listen 443 ssl http2;

    server_name www.domain.com; 

    #your configurations here
} 

The above configuration catches all HTTP requests for the domain.com and www.domain.com and redirects it to www.domain.com . It also catches the non-www HTTPS requests and redirect it to HTTPS with www.

Redirect to HTTPS with non-www

To redirect a url with HTTPS you need to install Let’s Encrypt SSL and you can add the below code inside your server block.

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

    server_name domain.com www.domain.com;

    return 301 https://domain.com$request_uri;
}

server {
    listen [::]:443 ssl http2;
    listen 443 ssl http2;

    server_name www.domain.com;

    ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem;
 
    root /var/www/html/domain.com/public/;
    index index.php index.html;

    return 301 https://domain.com$request_uri;
}

server {
    listen [::]:443 ssl http2;
    listen 443 ssl http2;

    server_name domain.com; 

    #your configurations here
}

The above configuration catches all HTTP requests for the domain.com and www.domain.com and redirects it to domain.com . It also catches the www HTTPS requests and redirect it to HTTPS without www.

Prepare yourself for a role working as an Information Technology Professional with Linux operating system

ADVERTISEMENT

Conclusion

Now you have learned how to redirect with www and without www and to HTTPS in Nginx.

Tags: NginxUbuntu 18.04
ShareTweetSendShare
Cloudbooklet

Cloudbooklet

Related Posts

How To Install Git On Ubuntu 20.04

How to Install Git on Linux

5 months ago
Vector Image

DataStax Enhances Astra DB on Google Cloud with Vector Search Capability

6 months ago
How To Setup Ssh Keys On Ubuntu

How to Setup SSH Keys on Ubuntu 20.04

6 months ago
Draggan Ai Editing Tool Install And Use Draggan Photo Editor

DragGAN AI Editing Tool Install and Use DragGAN Photo Editor

6 months ago

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Follow Us

Trending Articles

Screenshot Into Code

How to Convert Any Screenshot into Code (HTML/Tailwind/JS) in Minutes

November 20, 2023

CallBGProvider Stopping -How to Fix it in 7 Easy Ways

9 Free AI Clothes Remover Website: Remove Clothes from Photos Online

Wedding Dress AI: How to Use AI to Design Your Own Wedding Dress

Krea AI Live Painting: A New Feature to Unleash Your Creativity

PS5 Black Friday Deals 2023: Best Discounts and Bundles

Popular Articles

Biden-Xi Summit

Biden-Xi Summit: AI Weapons in Focus

November 14, 2023

Omegle shutdown: The Reasons and Their Solutions

Gen AI and Cybersecurity: How Firms are Facing the New Challenges

8 Best Free DeepNude AI Generators to Make AI DeepNudes

Nightshade AI: Data Poisoning Tool to Protect Artistic Integrity in the AI Era

OpenAI Launches GPT-4 Turbo New and Improved Language Model

Subscribe Now

loader

Subscribe to our mailing list to receives daily updates!

Email Address*

Name

Cloudbooklet Logo

Welcome to our technology blog, where we explore the latest advancements in the field of artificial intelligence (AI) and how they are revolutionizing cloud computing. In this blog, we dive into the powerful capabilities of cloud platforms like Google Cloud Platform (GCP), Amazon Web Services (AWS), and Microsoft Azure, and how they are accelerating the adoption and deployment of AI solutions across various industries. Join us on this exciting journey as we explore the endless possibilities of AI and cloud computing.

  • About
  • Contact
  • Disclaimer
  • Privacy Policy

Cloudbooklet © 2023 All rights reserved.

No Result
View All Result
  • News
  • Artificial Intelligence
  • AI Tools
  • Applications
  • Linux

Cloudbooklet © 2023 All rights reserved.