

In the world of SEO, one of the most common actions we apply to a project is redirects. From removing products from an online store to website changes, this mechanism is essential to prevent losing the accumulated ranking of one or more URLs.
In large websites, the process of preparing these redirects can take forever if many URLs have been modified and it hasn’t been carefully planned. Imagine, for example, redesigning a web directory with thousands of references where all the URLs of listings and pages have been changed. It would be practically impossible to create all the redirects manually.
In these types of scenarios, regular expressions come into play: through them, we can greatly speed up the task of generating redirects in large projects. Let's define what they are and look at real examples of using regular expressions for redirects.
Strictly speaking, redirects in the web environment are a group of HTTP response codes (specifically, the 300s). They are used to inform our browser that the URL we are requesting is no longer available, while providing us with a new URL to access. This process is usually transparent to the end user, as the browser handles (re)directing us to the new URL.
The 300 response codes are as follows:
In practice, when we talk about a redirect in the SEO field, we usually refer to 301 redirects. Why? They are the ones we typically need to use to transfer SEO value from one page to another, and they are recommended by Google for permanent redirects.
301 redirects consist of two parts:
Regular expressions, or RegEx, are patterns of different characters (letters, numbers, symbols) that we use to analyze a text string. For example, in a form where one of the fields is the DNI, we can use a regular expression to determine if it has the correct format. Or when a user enters a date, we can extract the year from it.
The combinations of patterns in regular expressions are infinite, and it’s hard to get lost when trying to set one up that’s useful for us. In practice, the simpler, the better, as long as the project we’re working on allows it. Some common patterns we can use in RegEx are:
We’ve already seen the definition of both regular expressions and redirects. But in practice, how can we use RegEx for our redirects? Depending on the type of website we have, we should choose one solution or another:
For this article, we will focus on redirects applied with the htaccess file. However, the logic behind them would be the same if we used another method.
In the case where our website has only changed domains, we can point all the URLs from the old domain to the new one with this rule in our htaccess:
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
The first part of the redirect contains the regular expression ^(.*)$When we have multiple subdomains (e.g., www), we may want to unify them all under the same subdomain or our main domain. Here we need to add a condition to apply the redirect only to the subdomains we are interested in:
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$ [NC]
RewriteRule ^(.*)$ https://mydomain.com/$1 [R=301,L]
Let’s say we want to redirect all URLs on our website that contain a specific word:
RewriteRule ^.*word.*$ /destination [R=301,L]
Now, let’s imagine that while working on an online shoe store, the URL for the Sports Shoes category has changed, from /shoes/sport/ to /shoes/sports/. And this affects all the product URLs in that category, like /shoes/sport/adidas-low-forum/. To manage these redirects with regular expressions, we can use the following code:
RewriteRule ^/shoes/sport/(.*)$ /shoes/sports/$1 [R=301,L]
These are just a few real examples of redirects using regular expressions. As mentioned earlier, the flexibility of regular expressions is practically infinite. But if you’re unsure and would prefer a professional team to help with your project, our SEO agency will be happy to assist you.

Hello! drop us a line
Forget about manual redirects thanks to regular expressions