You are on page 1of 1

mod_rewrite

Regular Expressions Syntax

Example: New domain

Start of string

#domain.com to domain2.com

End of string

RewriteCond

Any single character

RewriteRule

(a|b)

a or b

(...)

Group section

[abc]

Item in range (a, b or c)

[^abc]

Not in range (not a, b or c)

# domain.com/page.htm

\s

White space

RewriteRule

a?

Zero or one of a

a*

Zero or more of a

a*?

Zero or more of a, ungreedy

a+

One or more of a

# domain.com/category-name/

a+?

One or more of a, ungreedy

RewriteRule

a{3}

Exactly 3 of a

a{3,}

3 or more of a

a{3,6}

3 to 6 of a

a{3,6}?

3 to 6 of a, ungreedy

RewriteCond

%{HTTP_REFERER}

(viagra)

!(pattern)

"Not" prefix. Apply rule if

RewriteCond

%{HTTP_REFERER}

(xxx)

pattern does not match.

RewriteRule

%{HTTP_HOST}
^(.*)$

^www\.domain\.com$

http://www.domain2.com/$1

[NC]

[R=301,L]

Example: Page moved temporarily


to

^page.htm$

domain.com/new_page.htm
new_page.htm

[R,NC,L]

Example: Nice looking URLs (no querystring)


to

^([A-Za-z0-9-]+)/?$

domain.com/categories.php?name=category-name
categories.php?name=$1

[L]

Example: Block Referrer Spam (where referring URL contains "viagra" or "xxx" )

.*

[NC,OR]
[NC]

[F]

RewriteRule Flags

Server Variables: HTTP Headers

Server Variables: Time

R[=code]

Redirect to new URL, with

%{HTTP_USER_AGENT}

%{TIME_YEAR}

optional code (see below).

%{HTTP_REFERER}

%{TIME_MON}

Forbidden (sends 403

%{HTTP_COOKIE}

%{TIME_DAY}

header)

%{HTTP_FORWARDED}

%{TIME_HOUR}

Gone (no longer exists)

%{HTTP_HOST}

%{TIME_MIN}

Proxy

%{HTTP_PROXY_CONNECTION}

%{TIME_SEC}

Last Rule

%{HTTP_ACCEPT}

%{TIME_WDAY}

Next (i.e. restart rules)

Chain

T=mime-type

Set Mime Type

NS

Skip if internal sub-request

%{REMOTE_ADDR}

NC

Case insensitive

%{REMOTE_HOST}

%{API_VERSION}

QSA

Append query string

%{REMOTE_USER}

%{THE_REQUEST}

NE

Do not escape output

%{REMOTE_IDENT}

%{REQUEST_URI}

PT

Pass through

%{REQUEST_METHOD}

%{REQUEST_FILENAME}

S=x

Skip next x rules

%{SCRIPT_FILENAME}

%{IS_SUBREQ}

E=var:value

Set environmental variable

%{PATH_INFO}

"var" to "value".

%{QUERY_STRING}

%{TIME}
Server Variables: Request

%{AUTH_TYPE}

Server Variables: Special

Directives
RewriteEngine

RewriteCond Flags
Server Variables: HTTP Headers

RewriteOptions

NC

Case insensitive

OR

Combines rules with logical

%{DOCUMENT_ROOT}

RewriteLogLevel

"or" instead of "and".

%{SERVER_ADMIN}

RewriteLock

%{SERVER_NAME}

RewriteMap

%{SERVER_ADDR}

RewriteBase

%{SERVER_PORT}

RewriteCond
RewriteRule

Redirection Header Codes


301

Moved permanently

%{SERVER_PROTOCOL}

302

Moved temporarily

%{SERVER_SOFTWARE}

403

Forbidden

404

Not Found

410

Gone

RewriteLog

Available free from


AddedBytes.com

You might also like