How to use Regular Expressions in the redirect module

Regular expressions (regex) are used to match and manipulate text based on patterns. In the context of URL redirection, they can match multiple URLs and extract information from the source URL to copy it to the target URL.

This is particularly useful when you want to redirect URLs that share a common pattern. For example, you can use regex to redirect all URLs that match a certain pattern to a new location. 

 

Use with caution!
Understanding redirects is fundamental to ensure that changes to your website do not negatively impact your search engine rankings.

 

Here are some common examples how to use regex redirects in SiteManager:

  1. Redirect all pages from an old directory to a new subdirectory:
    • Source: /old-dir/(.*)
    • Destination: https://example.com/new-dir/$1
    •  Example:
      • Old: https://example.com/articles/welcome-to-our-new-house
      • New: https://example.com/blog/welcome-to-our-new-house
  2. Redirect a specific page to a new location:
    • Source: page1/?
    • Destination: https://example.com/page2/
    • Example:
      • Old: https://example.com/articles/
      • New: https://example.com/blog/
  3. Redirect indexed pages of single language (NL) to multi language site (NL, FR & EN): 
    • Source: (?!nl(\/|$)|fr(\/|$)|en(\/|$))(.*)
      • page doesn't start with nl/, fr/ or en/
    • Destination: https://example.com/nl/$1
    • Example:
      • Old: https://example.com/over-ons/
      • New: https://example.com/nl/over-ons/

Adding a wrong redirect to your website can cause it to break and result in a poor user experience. If you add a redirect that is incorrect or misconfigured, the browser is unable to load a webpage or even your whole website.

The error can negatively impact user experience and conversions, and it can also cause search engine crawlers to get trapped in the loop, which can hurt your website's SEO.

To avoid this issue, it's important to test your redirects thoroughly before implementing them on your website. You can use redirect checkers to ensure that your redirects are working correctly and not causing any issues.

Additionally, you should always keep a backup of your website in case something goes wrong with your redirects.