Learn in this tutorial how to redirect URLs when changing the WordPress category base with a little bit of code.
Have you ever been asked by a customer to change the category base in their WordPress?
You can imagine that depending on the amount of links to redirect, doing it manually can be a nightmare.
That’s why in this tutorial you will see how to do it using a regular expression that you must place in the .htaccess file.
But first a little bit of context.
In the “Permalinks” section of WordPress you can choose the base of the URLs category:
If you leave it empty, the URL that will be loaded before the categories would be /category/.
Therefore, you may want to change it to something that provides more contextual information.
The problem is that, in addition to changing the permalinks option, you have to make sure that the old URLs that are indexed in Google (and other sites) point to the new URL.
Let’s see how we can solve it in a simple way
Snippet to redirect URLs when changing the base of the categories in WordPress
Insert the following snippet at the beginning of the .htaccess file:
To see this and another 1097 code snippets of this website, login or subscribe here.
With this regular expression you redirect the visits that go to the version of the URL with the old category base to the new one.
For example, it will transform:
https://yourwebsite.com/old-slug-categories/
into:
https://yourwebsite.com/new-slug-categories/
Once a few months have passed, surely the search results will already show the URLs with the new structure and you can remove these lines of code from the .htaccess.
Although if the website has many links from social networks or other websites you can keep it.
Conclusion
You can see that thanks to regular expressions you can save a lot of time. Instead of performing 301 redirects manually, one by one, you can add a simple line of code in the .htaccess file.
It is a trick as simple as practical for WordPress designers and developers. Personally, it comes in handy for me. I hope you find it useful too.
Any questions? Let me know in the comments.
And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉