I just checked an .htaccess file with some custom redirects. There are two redirects I don't fully understand:

RedirectMatch 301 ^/news/([0-9]+)-(.*) /blog
RedirectMatch 301 ^/news/([a-z]+)-(.*)/$ /blog/$1

Both redirects are redirecting the subfolders or pages inside the news directory but I can't fully grab how because I don't understand the following parts [0-9]+)-(.*) and ([a-z]+)-(.*). What do they exactly mean?

3 Answers 3


The patterns you're confused about are nothing but regular expression functions. The ([0-9]+)-(.*) and ([a-z]+)-(.*) patterns each have two groups in them. Groups in regex are classified as anything within (). The contents of the group are then matched according to the criteria specified.

  1. In the group ([0-9]+) the criteria is to match one or more than one number. This does not include any alphabets or special characters
  2. In the group ([a-z]+) the criteria is to match one or more than one lower case characters. This does not include space, numbers, upper case characters etc.
  3. In the group (.*) the criteria is to match any character irrespective of whether it is an alphabet, number, special character etc.

In your .htaccess the following things are happening:

  1. RedirectMatch 301 ^/news/([0-9]+)-(.*) /blog
  2. RedirectMatch 301 ^/news/([a-z]+)-(.*)/$ /blog/$1

In statement 1 the pattern ^/news/([0-9]+)-(.*) is matched when the URL is example.com/news/23-article and is simply redirected to example.com/blog

In statement 2, the pattern ^/news/([a-z]+)-(.*)/$ is matched when the URL is example.com/news/latest-article/ and is redirected to example.com/blog/latest.

Notice the $1 in the destination URL in statement 2. The $1 is used to fetch the contents of group 1 that has matched, hence we get the destination URL as /blog/latest because the content of the first group is the word latest


These are regular expressions ("regex" for short). Specifically, Apache uses PCRE (Perl Compatible Regular Expression) syntax (the same as PHP, similar to JavaScript, etc.).

RedirectMatch 301 ^/news/([0-9]+)-(.*) /blog
RedirectMatch 301 ^/news/([a-z]+)-(.*)/$ /blog/$1

The RedirectMatch directive tries to match the regex (2nd argument) with the requested URL-path. If it matches then a redirect response is returned, to redirect the browser to the target URL (3rd argument).

The $1 backreference in the target URL of the second example copies text that has matched in the source URL-path. eg. Given a request for /news/abc-123/, the abc part is "copied" in order to redirect to /blog/abc (see later).


This basically matches 1 or more digits followed by a hyphen (followed by anything). Specifically:

  • [0-9] - This "character class" (denoted by [..]) matches a single character in the range 0-9, ie. a digit.
  • + is a "quantifier" and matches 1 or more of the preceding character/group. So it matches 1 or more digits.
  • - this hyphen is matched literally (it carries no special meaning when used outside of a character class).
  • .* matches any number of characters (except newline). Specifically . matches any character (except newline) and * is a quantifier that matches 0 or more of the preceding character/group. (Contrast with + which matches 1 or more.)
  • The parentheses (..) that surround parts of the regex make "capturing groups", which can be used later using backreferences. However, these are not being used here.

The above can be simplified (since none of the backreferences are used). We just need to match 1 or more digits, followed by a hyphen:


\d is a shorthand character class that matches digits. ie. the same as [0-9].


Very similar to the above, except it matches 1 or more lowercase letters (a-z), followed by a hyphen, followed by anything, before ending with a literal slash.

  • $ is an anchor signifying the end of the the string, ie. the end of the URL-path when used here. Conversely, ^ matches the start of the string, ie. the start of the URL-path (as opposed to some point in between).

Contrary to the first regex, the first backreference ($1) is used in this example, which captures whatever matches ([a-z]+). So, for example, /news/abc-123/ is redirected to /blog/abc.

This can't be simplified as much as the first regex, because of the capturing backreference and trailing slash. But the second group of parentheses could be removed:


I use tools like RegEx Buddy to help me with regular expressions as I don't use them enough to remember all of the syntax. It gives a visual explanation of regular expressions. Here's what it says about your regular expressions.


enter image description here


enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.