Skip to content

How to Redirect WordPress Slugs that Use Nginx Servers

How to Redirect WordPress Slugs that Use Nginx Servers

5 years ago, when we decided to create this blog, we had carefully planned how many categories we would create, one of which was the “How to” category. However, there was a small mistake we made when creating the category, which, as we progressed, became increasingly apparent and led us to realize we had to change it. We made a slight mistake by creating the slug for the “How-to” category with the slug “information”, which we ultimately want to return with the slug “how-to”.

The problem is, there are already quite a lot of articles in the “How-to” category. We are worried that just changing the slug will affect the SEO of this blog. But with careful consideration, we still do this to change the slug according to the category title, but we anticipate it by using a redirect. It becomes more complicated because we use an Nginx server, which, in reality, we are not too familiar with compared to handling an Apache web server.

With reckless capital, it turns out that we managed to redirect the old slug to the new slug, so that it will reduce the potential for visitors to see the not found page with code “404” when accessing the “information” slug. We documented this in writing so that those of you who experience the same thing will be able to handle it quickly.

Table of Contents

  1. What is an Nginx Server?
  2. What is a Slug?
  3. How to Redirect Slug or Path in Nginx
  4. Conclusion

What is an Nginx Server?

Nginx (pronounced “engine-x”) is a high-performance, open-source web server, reverse proxy server, load balancer, mail proxy, and HTTP cache. Originally created by Igor Sysoev to solve the “C10k problem” (handling 10,000+ concurrent connections efficiently), it has become one of the most popular web servers globally, powering many high-traffic sites like Netflix, Airbnb, WordPress.com, and Pinterest.

What is a Slug?

We use WordPress, which of course includes slug settings, both for categories and tags. A slug is the last, human-readable part of a webpage’s URL that specifically identifies that page or piece of content within a website or blog, typically derived from the title and formatted to be concise, readable, and SEO-friendly (e.g., in the URL https://www.waredata.com/category/how-to/, the slug is how-to). It uses lowercase letters, hyphens instead of spaces, and often omits stop words (like “a”, “the”, “is”) to create a clean, descriptive, and memorable web address that helps both users understand the page’s topic and search engines index the content effectively.

How to Redirect Slug or Path in Nginx

Before redirecting, of course, you have to understand where to put the redirect command in Nginx; here are some locations that can be used to redirect the old slug to the new one.

/etc/nginx/nginx.conf – main config (often includes others)
/etc/nginx/sites-available/your-site.conf – typical on Ubuntu/Debian
/usr/local/nginx/conf/nginx.conf – typical on custom installs

or

In our case, we have an interface or panel that can be used to put a redirect script, so the conditions may not be the same; those who have the same case may have to ask your Administrator or your blog hosting service provider.

Now that you know where to put the redirect script, let’s move on to the implementation. Here we want every visitor who visits “https://www.waredata.com/category/information/” to be redirected to “https://www.waredata.com/category/how-to/”. If your case is the same, you can use the following redirect script.

location = /category/information/ {
    return 301 https://www.waredata.com/category/how-to/;
}

or

rewrite ^/category/information/$ https://www.waredata.com/category/how-to/ permanent;

The problem is, what if there is a slug or path like this: https://www.waredata.com/category/information/page/3/ so that “/information/” also changes to “/how-to/”? If your case is similar to this one, you can add the following script.

location ^~ /category/information/ {
    rewrite ^/category/information/(.*)$ /category/how-to/$1 permanent;
}

Another case, when we open https://www.waredata.com/category/information without “/” at the end, it won’t become https://www.waredata.com/category/how-to/, how?

By default, Nginx treats /category/information and /category/information/ as different URIs, so our existing redirect likely misses the one without the trailing slash.

To handle both versions – with and without the trailing slash – and redirect them properly to /category/how-to equivalents, we can use this complete Nginx configuration snippet:

# Match without trailing slash
location = /category/information {
    return 301 https://www.waredata.com/category/how-to/;
}
# Match the exact path with slash
location = /category/information/ {
    return 301 https://www.waredata.com/category/how-to/;
}
# Match anything under /category/information/ (e.g., page/3, post, etc.)
location ^~ /category/information/ {
    rewrite ^/category/information/(.*)$ /category/how-to/$1 permanent;
}

In our case, we only use these 2 redirects to be able to handle everything.

# Match anything under /category/information/ (e.g., page/3, post, etc.)
location ^~ /category/information/ {
    rewrite ^/category/information/(.*)$ /category/how-to/$1 permanent;
}
# Match without trailing slash
location = /category/information {
    return 301 https://www.waredata.com/category/how-to/;
}

Conclusion

Even if we only change the old slug to the new slug a small amount, it’s important to implement this so that search engines and visitors can notice it immediately.

If you use a cache, you may need to clear the cache or use the browser in incognito mode to see the effect immediately.

Maybe you like other interesting articles?

Leave a Reply

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