Defining Regex Characters for .htaccess

Regex or a regular expression is typically defined as a specific string of text used to describe a search pattern. Essentially regular expressions can be thought of as really powerful wildcards. Whether you know it or not regular expressions or regexes are used anytime you perform a search on the internet through a search engine such as Google or use the find and replace function in any text editor. The focus of this article is to review the specific regex character definitions when working with .htaccess.


.htaccess regex character definitions

The following table showcases the different regex characters that are available to you when dealing with a .htaccess file.


Regex Character Explanation
# Forces the server to ignore the text following the # on the same line typically used for comments
[F] Indicates Forbidden the server should return a 403 forbidden error to the client
[L] The Last rule forces the server any further rewriting
[N] Indicates Next and forces Apache to keep running rewrite rules until they have been archived
[G] Gone tells the server deliver the gone status message
[R] This forces Apache to initialize a redirect
[P] Indicates Proxy which tells the server to use mod_proxy to handle requests
[C] Tells the server to chain a rule with a previous rule
[QSA] Tells the server to use the query string at the end of an expression
[NC] No Case instructs the server to treat any argument as case insensitive
[NS] The No Subrequest forces the server to skip if it is an internal sub request
[PT] Pass Through has mod_rewrite send a formatted URL back to Apache
[NE] No Escape forces the server to parse through all output ignoring escaping characters
[OR] Specifies a logical or statement that evaluates two expressions
[S=x] Forces the server to skip “x” number of rules based on if a match is found
[a-z] Denotes a range of characters between the two characters separated by a dash
[^] Defines not within a character class
[]+ Defines that any combination characters defined within the brackets is a match there can be multiple matches
[] Defines that any characters defined within the brackets is a match
[T=MIME-type] Defines the mime type
[E=variableName:newValue] Forces the server to set the environmental variable “variableName” to the value “newValue”
a{n} Defines the specific number of the preceding character to be matched
? Defines the preceding character as being optional
$ Signals the end of a regular expression
() Can be used to group characters together
^ Signals the beginning of a regular expression
. Specifies a single arbitrary character
Signals not to perform an action
! Defines negation
+ Will match at least one preceding character
| Logical or operator
* Wildcard that will match any occurrence of the preceding character
\. Signals an escaped literal period
\ Used to escape special characters
-d Analyzes if a string exists within a directory
-f Determines if a string is a preexisting file
-s Tests for a non zero value



Leave a Reply