HomeCoding & Programming

Definition of .htaccess Regex Characters

Like Tweet Pin it Share Share Email

Definition of .htaccess Regex Characters & htaccess regular expression special characters

 

#
The # instructs the server to ignore the line. It is used for including comments. Each line of comments requires its own #. When including comments, it is good practice to use only letters, numbers, dashes, and underscores. This will help eliminate/avoid potential server parsing errors.

 

[F]
Forbidden: Instructs the server to return a 403 Forbidden to the client or browser.

 

[L]
Last rule: Instructs the server to stop rewriting after the preceding directive is processed.

 

[N]
Next: Instructs Apache to rerun the rewrite rule until all rewriting directives have been achieved.

 

[G]
Gone: Instructs the server to deliver Gone (no longer exists) status message.

 

[P]
Proxy: Instructs server to handle requests by mod_proxy

 

[C]
Chain: Instructs server to chain the current rule with the previous rule.

 

[R]
Redirect: Instructs Apache to issue a redirect, causing the browser to request the rewritten/modified URL.

 

[NC]
No Case: Defines any associated argument as case-insensitive.

 

[PT]
Pass Through: Instructs mod_rewrite to pass the rewritten URL back to Apache for further processing.

 

[OR]
Or: Specifies a logical “or” that ties two expressions together such that either one proving true will cause the associated rule to be applied.

 

[NE]
No Escape: Instructs the server to parse output without escaping characters.

 

[NS]
No Sub request: Instructs the server to skip the directive if internal sub-request.

 

[QSA]
Append Query String: Directs server to add the query string to the end of the expression (URL).

 

[S=x]
Skip: Instructs the server to skip the next “x” number of rules if a match is detected.

 

[E=variable: value]
Environmental Variable: Instructs the server to set the environmental variable “variable” to “value”.

 

[T=MIME-type]
Mime Type: Declares the mime type of the target resource.

 

[]
Specifies a character class, in which any character within the brackets will be a match. e.g., [xyz] will match either an x, y, or z.

 

[]+
Character class in which any combination of items within the brackets will be a match. e.g., [xyz]+ will match any number of x’s, y’s, z’s, or any combination of these characters.

 

[^]
Specifies not within a character class. e.g., [^xyz] will match any character that is neither x, y, nor z.

 

[a-z]
A dash (-) between two characters within a character class ([]) denotes the range of characters between them. e.g., [a-zA-Z] matches all lowercase and uppercase letters from a to z.

 

a{n}
Specifies an exact number, n, of the preceding character. e.g., x{3} matches exactly three x’s.

 

a{n,}
Specifies n or more of the preceding character. e.g., x{3,} matches three or more x’s.

 

a{n,m}
Specifies a range of numbers, between n and m, of the preceding character. e.g., x{3,7} matches three, four, five, six, or seven x’s.

 

()
Used to group characters together, thereby considering them as a single unit. e.g., (perishable)?press will match press, with or without the perishable prefix.

 

^
Denotes the beginning of a regex (regex = regular expression) test string. i.e., begin argument with the proceeding character.

 

$
Denotes the end of a regex (regex = regular expression) test string. i.e., end argument with the previous character.

 

?
declares as optional the preceding character. e.g., monzas? will match monza or monzas, while mon(za)? will match either mon or monza. i.e., x? matches zero or one of x.

 

!
Declares negation. e.g., “!string” matches everything except “string”.

 

.
A dot (or period) indicates any single arbitrary character.

 


Instructs “not to” rewrite the URL, as in “…domain.com.* – [F]”.

 

+
Matches one or more of the preceding character. e.g., G+ matches one or more G’s, while “+” will match one or more characters of any kind.

 

*
Matches zero or more of the preceding character. e.g., use “.*” as a wildcard.

 

|
Declares a logical “or” operator. for example, (x|y) matches x or y.

 

\
Escapes special characters ( ^ $ ! . * | ). e.g., use “\.” to indicate/escape a literal dot.

 

\.
Indicates a literal dot (escaped).

 

/*
zero or more slashes.

 

.*
Zero or more arbitrary characters.

 

^$
Defines an empty string.

 

^.*$
The standard pattern for matching everything.

 

[^/.]
Defines one character that is neither a slash nor a dot.

 

[^/.]+
Defines any number of characters which contains neither slash nor dot.

 

http://
This is a literal statement — in this case, the literal character string, “http://”.

 

^domain.*
Defines a string that begins with the term “domain”, which then may be proceeded by any number of any characters.

 

^domain\.com$
Defines the exact string “domain.com”.

 

-d
Tests if string is an existing directory

 

-f
Tests if string is an existing file

 

-s
Tests if file in test string has a non-zero value

 

Hope the above will help you a lot, if you have any issue in htaccess rules…post your valuable comment below.