How to add Nginx rewrite rules using SSH

Are you looking for a way on how to add Nginx rewrite rules using SSH on an Ubuntu server?

How to add Nginx rewrite rule using SSH

Rewrite rules are used to tell the server to redirect users from one URL to another. This is useful for a variety of reasons, such as consolidating multiple pages into one or redirecting users to a different page based on their location.

Related: How to fix the ERR_TOO_MANY_REDIRECTS on Plesk

Steps to add Nginx rewrite rules using SSH

  1. Log in to your server using SSH.
  2. Then open the Nginx configuration file for your website. This file is usually located in the /etc/nginx/sites-available directory, and it may be named something like default or example.com. Use a text editor to open the file
sudo nano /etc/nginx/sites-available/example.com
  1. Scroll down to the server block in the configuration file. This is where you will add your rewrite rules.
  2. To add a rewrite rule, use the rewrite directive. This is an example of a basic rewrite rule that redirects users from example.com/old-page to example.com/new-page:
rewrite ^/old-page$ /new-page permanent;
  1. You can also use regular expressions in your rewrite rules. For example, to redirect all pages with a .php extension to their equivalent .html page, you can use a rule like this:
rewrite ^(.*).php$ $1.html permanent;
  1. Once you have added your rewrite rules, save the configuration file and exit your text editor.
  2. Now test your changes by running the following command:
sudo nginx -t

If the test passes, reload Nginx to apply your changes:

sudo systemctl reload nginx

And that’s it! You can also utilize this method to add rewrite rules for Rank Math on your Ubuntu server.

Disclosure: We may earn commission for purchases that are made by visitors on this site at no additional cost on your end. All information is for educational purposes and is not intended for financial advice. Read our affiliate disclosure.

Share this:

Similar Posts

  • Droip 2.0 WordPress Website Builder Review

    Droip 2.0 WordPress Website Builder is a cutting-edge, no-code website builder designed specifically for WordPress users who demand both power and ease of use. With a complete overhaul in its user interface and a suite of advanced features, Droip 2.0 aims to simplify the website design process while offering unparalleled control over every design element….

  • How to Utilize Geo-Blocking to Control What Your Visitors Access

    How to Utilize Geo-Blocking to Control What Your Visitors Access A comprehensive guide to implementing geo-blocking technology, protecting your content, and enhancing user experience based on location Table of Contents 1. Introduction to Geo-Blocking 2. How Geo-Blocking Works 3. Benefits of Implementing Geo-Blocking 4. Implementation Methods 4.1. JavaScript Implementation 4.2. PHP Implementation 4.3. AWS CloudFront…

  • How to submit a sitemap to Google Search Console

    Submitting a sitemap to Google Search Console helps to improve your website’s visibility in search results. Google can discover your website, however, it is good to submit a sitemap to increase the speed of being indexed by various search engines. A sitemap is simply a file that lists all of the pages on your website,…

  • How to fix cURL error 7 failed to connect to Port 443

    If you have encountered a “cURL error 7 failed to connect to Port 443” you know how stressful it is to figure out a solution. It causes a website to have a poor security check score altering your site’s performance. I once encountered a “cURL error 7 failed to connect to Port 443” on one…

  • 9 Best WordPress caching plugins for improving site speed

    Are you looking for the best WordPress caching plugins? Caching is an essential aspect of website performance, as it helps to reduce the load on the server and speed up the delivery of content to users. If you’re using WordPress to power your website, you have a range of caching plugins to choose from that…

  • Kadence AI Review: Unveiling the Power of This SEO-Friendly Tool

    Kadence AI is a revolutionary feature that has transformed the process of creating and customizing websites using the Kadence Theme for WordPress. The AI-powered web creation process offers several innovative features that streamline website development and design. One of the standout features of Kadence AI is its “AI-Powered Inline Content,” which empowers users to improve…

Leave a Reply

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