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

  • Geo Targetly Review: The Ultimate Geolocation Solution

    Geo Targetly Review: The Ultimate Geolocation Solution for Discover how Geo Targetly can transform your website’s performance by delivering personalized experiences based on visitor location. Try Geo Targetly Free for 14 Days Show Table of Contents Introduction to Geo Targetly Key Features & Functionality How Geo Targetly Works Benefits for Businesses Pricing Plans Comparison User…

  • WP Rocket Review: How to Speed Up Your WordPress Website

    If you’re looking to speed up your WordPress website, WP Rocket is a caching plugin that comes highly recommended. In this comprehensive WP Rocket review, we’ll explore how to effectively use this powerful tool to enhance your site’s performance. We’ll cover everything from installation to advanced settings that can help you achieve lightning-fast load times….

  • How to add H1 tag in Kadence theme

    Are using the Kadence theme and are you getting an H1 tag error when you run an SEO analysis on your website? This error simply indicates that you have not added an H1 tag in Kadence theme. In this article, you will learn how to add H1 tag in Kadence theme. Search engines use the…

  • How to exclude a post from the Related Posts block

    How to disable a specific post from showing in the Kadence Theme Related Posts block To disable a specific post from showing in the related posts block at the end of each post in WordPress with the Kadence theme, you can follow these steps: Remember, always backup your site before making any changes to the…

  • How to build backlinks for Christian websites

    Backlinks are one of the most important factors for ranking in search engines. This is true for all websites, including Christian blogs and church websites. Search engines use them to determine the relevance and value of a web page. I will show you how to build backlinks for Christian websites and churches. Backlinks help to…

  • How to optimize your WordPress site: The Easy way

    Is your website slow and looking for a way on how to optimize your WordPress site to improve performance? Optimizing the speed and performance of your WordPress website is important for a number of reasons. A fast-loading website not only provides a better user experience, but can also improve your search engine rankings, increase conversions,…

Leave a Reply

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