Using RedirectMatch in Your .htaccess File

Here is a quick overview of how to redirect URLs in your htaccess file using RedirectMatch (regex).

Firstly, here’s an example of a RedirectMatch redirect:

RedirectMatch 301 ^/services/?$ https://www.andycorby.com/

Now let’s break this down:

  • RedirectMatch – This states we want to base the redirect on a regular expression (also known as regex), the redirect will occur if a URL matches the regex.
  • 301 – Type of redirect. 301 is a permanent redirect (stating the page has moved forever).
  • Regex. Here are the symbols used in this example and their meanings.
    • ^ – Match anything before this character
    • ? – Match with or without the previous character, so in this example it would match /services or /services/
    • $ – End of url, do not match anything after this character.
  • URL – The destination URL where you’d like traffic to any pages that meet the regex condition redirected to.

I hope this is helpful.

Leave a Reply

Your e-mail address will not be published. Required fields are marked *

18 − 4 =