Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Web Servers»Nginx»Redirecting URLs in NGINX

    Redirecting URLs in NGINX

    By RahulMarch 3, 20233 Mins Read

    NGINX is a powerful open-source web server that can be used for various purposes, including URL redirection. URL redirection is the process of forwarding one URL to another. It is a common practice to redirect URLs when you change the URL structure of your website or when you move content from one page to another.

    Advertisement

    In this article, we will discuss URL redirection in NGINX and provide some examples to help you understand how it works.

    Types of URL Redirection

    There are two types of URL redirection:

    • 301 Redirect: A 301 redirect is a permanent redirection that tells search engines and browsers that the URL has permanently moved to a new location. It is commonly used when you change the URL structure of your website or when you move content from one page to another.
    • 302 Redirect: A 302 redirect is a temporary redirection that tells search engines and browsers that the URL has moved temporarily to a new location. It is commonly used when you need to temporarily redirect traffic to a different page.

    Redirecting HTTP to HTTPS

    One of the most common uses of URL redirection in NGINX is redirecting HTTP traffic to HTTPS. HTTPS is a secure version of the HTTP protocol that encrypts data between the server and the browser. To redirect HTTP traffic to HTTPS, add the following code to your NGINX configuration file:

    1
    2
    3
    4
    5
    server {
        listen 80;
        server_name example.com;
        return 301 https://example.com$request_uri;
    }

    This code listens on port 80 and redirects all HTTP traffic to HTTPS by returning a 301 status code.

    Redirecting Non-WWW to WWW URLs

    If you want to redirect non-www URLs to their www counterparts, add the following code to your NGINX configuration file:

    1
    2
    3
    4
    5
    server {
        listen 80;
        server_name example.com;
        return 301 $scheme://www.example.com$request_uri;
    }

    This code listens on port 80 and redirects all non-www URLs to their www counterparts by returning a 301 status code.

    Redirecting WWW to Non-WWW URLs

    If you want to redirect www URLs to their non-www counterparts, add the following code to your NGINX configuration file:

    1
    2
    3
    4
    5
    server {
        listen 80;
        server_name www.example.com;
        return 301 $scheme://example.com$request_uri;
    }

    This code listens on port 80 and redirects all www URLs to their non-www counterparts by returning a 301 status code.

    Redirecting a Specific URL to Another URL

    If you want to redirect a specific URL to another URL, add the following code to your NGINX configuration file:

    1
    2
    3
    location /old-url {
        return 301 https://example.com/new-url;
    }

    This code redirects all traffic from the /old-url to https://example.com/new-url.

    Custom 404 Error Page and Redirect

    To create a custom 404 error page and redirect, add the following code to your NGINX configuration file:

    1
    2
    3
    4
    error_page 404 /404.html;
    location = /404.html {
        internal;
    }

    This code redirects all 404 errors to the custom 404.html page.

    Conclusion

    URL redirection is a powerful tool that can be used to redirect traffic from one URL to another. In this article, we discussed the two types of URL redirection and provided some examples to help you understand how it works in NGINX. By understanding URL redirection, you can create a better user experience for your visitors and improve the overall performance of your website.

    nginx
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Configuring Nginx for Laravel on Ubuntu & Debian

    Configuring Nginx to Handle 100 Thousands Request Per Minute

    Configuring Nginx to Handle 100 Thousands Request Per Minute

    How To Set Up Nginx Reverse Proxy: A Step-By-Step Tutorial

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Implementing a Linux Server Security Audit: Best Practices and Tools
    • cp Command in Linux (Copy Files Like a Pro)
    • 15 Practical Examples of dd Command in Linux
    • dd Command in Linux (Syntax, Options and Use Cases)
    • Iptables: Common Firewall Rules and Commands
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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