Regular Expressions


Hosting always uses the latest version of Apache. Apache 2.4.53 fixed a long-standing bug that could cause deliberately incorrect regular expressions to work. This could affect the operation of sites that were created for older versions of Apache and which have the .htaccess there are invalid regular expressions. There are several ways to solve the problem: find and fix problematic regular expressions on your own, contact the site developer for help, try updating the CMS to the latest version, in which the problem could be fixed.
It is convenient to test and debug regular expressions using services like regex101.com or regexr.com.

In directives .htaccess you can use perl-like regular expressions for flexible customization of rules. Regular expressions are usually used in conjunction with variables web server. Regular expression example:

RedirectMatch /([^/]*)/([^/]*)/script.php$ http://example.com/index.php?$1=$2
  • . — replaces any one character.
  • ? — indicates the optional use of the previous character (for example, in the construction tes?t will fit like words test so and tet).
  • * — means that the previous character (group of characters) maybe repeat (this symbol indicates an optional repetition, that is, the symbol can occur either 0 times or an infinite number).
  • [abc] — indicates a list of characters that match letters a, b or with.
    • [^abc] — a list of symbols that are not used. That is, any character will do except a, b or with.
    • [abc]* — a list of consecutive characters that will be found in the string.
    • [^abc]* — a list of unacceptable consecutive characters in a string.
  • + — means that the previous character (group of characters) should repeat (as opposed to *, this symbol indicates a mandatory repetition, that is, the symbol can occur at least 1 time).
  • ! — the logical NOT symbol, used to form exclusion rules.
  • | — logical OR symbol, set in groups.
  • () — grouping of structures.
  • {x} — repeating a character several times, where X — the number of repetitions (several options must be set separated by commas).
  • \ — escaping of service characters for their use in templates.
  • .* — specifying any number of characters in any order. /.*/ — in this case, all substrings between slashes will match.
  • ^ — the beginning of a line, used at the beginning of an expression.
  • $ — end of line, indicated at the end of the line.
  • \w — indicates a letter, number, or underscore _.
  • \W — any character other than letters, numbers and underscore.
  • \d — indicates any digit.
  • \D — indicates any character other than numbers.
  • \s — any space character (indentation).
  • \S — any non—whitespace character.

Sample from ASCII table:

  • [0-9] — a range of numbers from 0 to 9.
  • [a-z] — a range of letters from a to z, lowercase Latin characters.
  • [A-Z] — a range of letters from A to Z in uppercase.
  • [a-zA-Z] or [a-Z] — a range of letters from a to Z in any case.


Lexical comparison is a comparison of characters in the order in which they are specified (in lexicographical order), which means that 12 less than 2.

  • < — Lexically less.
  • > — Lexically more.
  • <= — Lexically less than or equal to.
  • >= — Lexically greater than or equal to.
  • = — Lexically equal.
  • != — Lexically not the same.

Numerical comparison:

  • -lt — Less.
  • -gt — More.
  • -le — Less or equal.
  • -ge — More or equal.
  • -eq — Equals.
  • -ne — Not equal.

Target Path Information:

  • -d — Whether the target path is a directory.
  • -f — Whether the target path is a file.
  • -s — Whether the target path is a file with a size greater than 0.
  • -l — Whether the target path is a symbolic link.
  • -x — Whether the target path has execution rights.

For example, two URLs:

  1. http://example.com/someurl/44/test/regular/expression.php
  2. http://example.com/url/4/another/test/somefile.php

To create a redirect template that will include all requests that start with someurl, you need to specify it like this:

RewriteCond %{REQUEST_URI} ^/someurl/

In this case, both addresses will fall under the rule:

RewriteCond %{REQUEST_URI} ^/.*url/

Using the OR value, you can achieve rules for several options:

RewriteCond %{REQUEST_URI} ^/(someurl|url)/

Using range and repetition, you can achieve rules for enumerating suitable values, the example will print the first address:

RewriteCond %{REQUEST_URI} ^(/+)[0-9]{2}(/+) 

An example like this will print the second address:

RewriteCond %{REQUEST_URI} ^(/+)[0-9](/+) 

To exclude a link from the request using regular, you should use the following rule:

RewriteCond %{REQUEST_URI} !/regular/