mod rewrite cheat sheet

Page 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

C

Chained with next rule

%{HTTP_USER_AGENT}

%{TIME_YEAR}

CO=cookie

Set specified cookie

%{HTTP_REFERER}

%{TIME_MON}

E=var:value

Set environmental variable

%{HTTP_COOKIE}

%{TIME_DAY}

“var” to “value”

%{HTTP_FORWARDED}

%{TIME_HOUR}

F

Forbidden (403 header)

%{HTTP_HOST}

%{TIME_MIN}

G

Gone - no longer exists

%{HTTP_PROXY_CONNECTION}

%{TIME_SEC}

H=handler

Set handler

%{HTTP_ACCEPT}

%{TIME_WDAY}

L

Last - stop processing rules

N

Next - continue processing

NC

Case insensitive

NE

Do not escape output

%{REMOTE_ADDR}

NS

Ignore if subrequest

%{REMOTE_HOST}

%{API_VERSION}

P

Proxy

%{REMOTE_USER}

%{THE_REQUEST}

PT

Pass through

%{REMOTE_IDENT}

%{REQUEST_URI}

R[=code]

Redirect to new URL, with

%{REQUEST_METHOD}

%{REQUEST_FILENAME}

optional code (see below)

%{SCRIPT_FILENAME}

%{IS_SUBREQ}

QSA

Append query string

%{PATH_INFO}

S=x

Skip next x rules

%{QUERY_STRING}

T=mime-type

Set mime type

%{AUTH_TYPE}

%{TIME} Server Variables: Request Server Variables: Special

Directives RewriteEngine RewriteOptions

RewriteCond Flags

Server Variables: HTTP Headers

NC

Case insensitive

%{DOCUMENT_ROOT}

RewriteLogLevel

OR

Combines rules with logical

%{SERVER_ADMIN}

RewriteLock

"or" instead of "and".

%{SERVER_NAME}

RewriteMap

%{SERVER_ADDR}

RewriteBase

%{SERVER_PORT}

RewriteCond

%{SERVER_PROTOCOL}

RewriteRule

Redirection Header Codes 301

Moved permanently

302

Moved temporarily (default)

RewriteLog

%{SERVER_SOFTWARE} Available free from AddedBytes.com


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.