/([a-z-0-9-]*) php
RedirectMatch 301 ^/news/([0-9]+)-(.*) /blog
RedirectMatch 301 ^/news/([a-z]+)-(.*)/$ /blog/$1
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).
([0-9]+)-(.*)
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+-
\d is a shorthand character class that matches digits. ie. the same as [0-9].
([a-z]+)-(.*)/$
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:
([a-z]+)-.*/$
0
8
RedirectMatch 301 ^/news/([0-9]+)-(.*) /blog
RedirectMatch 301 ^/news/([a-z]+)-(.*)/$ /blog/$1
Thank you!
8
0
Are there any code examples left?
New code examples in category PHP
-
PHP 2022-03-27 22:40:03 index:of curiosa 2019 mp4
-
PHP 2022-03-27 22:35:13 Dijkstra’s algorithm php
-
PHP 2022-03-27 22:35:12 get_declared_classes
-
PHP 2022-03-27 22:30:23 php move index of a value to first position in array
-
PHP 2022-03-27 22:25:01 wordpress get_date
-
PHP 2022-03-27 21:30:38 php shorten string with dots
-
PHP 2022-03-27 21:10:46 php header 500
-
PHP 2022-03-27 21:05:06 laravel schedule run
-
PHP 2022-03-27 20:25:12 Get page title, excerpt or content by id
-
PHP 2022-03-27 20:00:27 php convert date from dd/mm/yyyy to yyyy-mm-dd