2.7.1.1.11. Regular expressions
Attention!
Our hosting always uses the latest version of Apache. In Apache 2.4.53, a long-standing bug was fixed that allowed known-incorrect regular expressions to work. This could have affected sites that were created for older versions of Apache and have incorrect regular expressions in their .htaccess file. There are several ways to resolve the issue: find and fix the problematic regular expressions yourself, contact the site developer for assistance, or try updating the CMS to the latest version, in which the issue may have been fixed.In .htaccess directives, you can use Perl-like regular expressions to flexibly configure rules. Regular expressions are typically used in conjunction with web server variables. Example of a regular expression:
RedirectMatch /([^/]*)/([^/]*)/script.php$ http://example.com/index.php?$1=$2
Syntax
.— matches any single character.?— indicates that the preceding character is optional (for example, in the patterntes?t, bothtestandtetwould match).*— means that the preceding character (or group of characters) may be repeated (this character indicates an optional repetition, meaning the character may appear zero times or an infinite number of times).[abc]— specifies a list of characters that match the lettersa,b, orс.[^abc]— a list of characters that are not used. In other words, any character excepta,b, orс.[abc]*— a sequence of consecutive characters that will be found in the string.[^abc]*— a list of characters that cannot appear consecutively in a string.
+— means that the preceding character (or group of characters) must be repeated (unlike*, this character indicates a mandatory repetition, meaning the character must appear at least once).!— logical NOT operator, used to define exception rules.|— logical OR operator, used within groups.()— grouping of structures.{x}— repeat character several times, whereXis the number of repetitions (separate multiple options with commas).\— escaping special characters for use in templates..*— any number of characters in any order./.*/— in this case, any substring between the slashes will match.
^— start of a line, used at the beginning of an expression.$— end of line, indicated at the end of the line.
\w— specifies a letter, a number, or an underscore_.\W— any character other than letters, numbers, and the underscore.\d— indicates any number.\D— indicates any character other than a digit.\s— any whitespace character (space).\S— any non-space character.
Selection from the ASCII table:
[0-9]— the range of digits from 0 to 9.[a-z]— the range of letters from a to z, lowercase Latin characters.[A-Z]— the range of letters from A to Z in uppercase.[a-zA-Z]or[a-Z]— the range of letters from a to Z in any case.
Conditions
A lexical comparison is a comparison of characters in the order in which they appear (in lexicographical order), which means that 12 is less than 2.
<— lexically less.>— lexically greater.<=— lexically less than or equal to.>=— lexically greater than or equal to.=— lexically equal.!=— lexically unequal.
Numerical comparison:
-lt— less.-gt— greater.-le— less than or equal to.-ge— greater than or equal to.-eq— equal.-ne— unequal.
Information about the target path:
-d— is the target path a directory?-f— is the target path a file?-s— is the target path a file larger than 0?-l— is the target path a symbolic link?-x— does the target path have execution permissions?
Examples of use
Here are two URLs as examples:
http://example.com/someurl/44/test/regular/expression.phphttp://example.com/url/4/another/test/somefile.php
To create a redirect rule that will capture all requests beginning with someurl, specify it as follows:
RewriteCond %{REQUEST_URI} ^/someurl/
In that case, both addresses will be covered by the rule:
RewriteCond %{REQUEST_URI} ^/.*url/
By using the OR operator, you can define rules for multiple options:
RewriteCond %{REQUEST_URI} ^/(someurl|url)/
By using a range and repetition, you can establish rules for iterating through suitable values; the example will output the first address:
RewriteCond %{REQUEST_URI} ^(/+)[0-9]{2}(/+)
And this example will output the second address:
RewriteCond %{REQUEST_URI} ^(/+)[0-9](/+)
To exclude a link containing regular from the query, use the following rule:
RewriteCond %{REQUEST_URI} !/regular/