Complete Cheatsheet of Mod_rewrite

Below is the Complete Cheatsheet of Mod_rewrite

RewriteRule Flags

  • C
  • Chained with next rule
  • CO=cookie
  • Set specified cookie
  • E=var:-value
  • Set enviro-nmental variable “var” to “value”
  • F
  • Forbidden (403 header)
  • G
  • Gone – no longer exists
  • H=handler
  • Set handler
  • L
  • Last – stop processing rules
  • N
  • Next – continue processing
  • NC
  • Case insens-itive
  • NE
  • Do not escape output
  • NS
  • Ignore if subrequest
  • P
  • Proxy
  • PT
  • Pass through
  • R[=code]
  • Redirect to new URL, with optional code (see below)
  • QSA
  • Append query string
  • S=x
  • Skip next x rules
  • T=mime–type
  • Set mime type

RegExp

Regular Expressions Syntax

  • ^
  • Start of string
  • $
  • End of string
  • .
  • Any single character
  • (a|b)
  • a or b
  • (…)
  • Group section
  • [abc]
  • In range (a, b or c)
  • [^abc]
  • Not in range
  • \s
  • White space
  • a?
  • Zero or one of a
  • a*
  • Zero or more of a
  • a*?
  • Zero or more, ungreedy
  • a+
  • One or more of a
  • a+?
  • One or more, ungreedy
  • a{3}
  • Exactly 3 of a
  • a{3,}
  • 3 or more of a
  • a{,6}
  • Up to 6 of a
  • a{3,6}
  • 3 to 6 of a
  • a{3,6}?
  • 3 to 6 of a, ungreedy
  • \
  • Escape character
  • [:punct:]
  • Any punctu-ation symbol
  • [:space:]
  • Any space character
  • [:blank:]
  • Space or tab

Directives

  • Rewrit-eEngine
  • Rewrit-eOp-tions
  • RewriteLog
  • Rewrit-eLo-gLevel
  • Rewrit-eLock
  • RewriteMap
  • Rewrit-eBase
  • Rewrit-eCond
  • Rewrit-eRule

Miscellaneous

RewriteCond Flags

  • NC
  • Case insens-itive
  • OR
  • Combine with next rule using ‘OR’ instead of the default of ‘AND’

Redirection Header Codes

  • 301
  • Moved perman-ently
  • 302
  • Moved tempor-arily (default)

Sample Rule: Site Moved

  • # Site moved perman-ently
  • Rewrit-eCond %{HTTP-_HOST} ^www.d-oma-in.com$ [NC]
  • Rewrit-eRule ^(.*)$ http:/-/ww-w.d-oma-in2.com/$1 [R=301,L]
  • Rewrites domain.com to domain-2.com

Sample Rule: Temporary Page Move

  • # Page has moved tempor-arily
  • Rewrit-eRule ^page.h-tml$ new_pa-ge.html [R,NC,L]
  • Rewrites domain.co-m/p-age.html to domain.co-m/n-ew_-pag-e.html
READ  PHP Basics Tutorial

Sample Rule: Nice URLs

  • # Nice URLs (no query string)
  • Rewrit-eRule ^([A-Z-a-z-0-9–]+)/?$ catego-rie-s.p-hp?-name=$1 [L]
  • Rewrites domain.co-m/c-ate-gor-y-n-ame-1/ to domain.co-m/c-ate-gor-ies.ph-p?n-ame-=ca-teg-ory–name-1

HTTP Headers

  • %{HTTP-_US-ER_-AGENT}
  • %{HTTP-_RE-FERER}
  • %{HTTP-_CO-OKIE}
  • %{HTTP-_FO-RWA-RDED}
  • %{HTTP-_HOST}
  • %{HTTP-_PR-OXY-_CO-NNE-CTION}
  • %{HTTP-_AC-CEPT}

Server Internals

  • %{DOCU-MEN-T_ROOT}
  • %{SERV-ER_-ADMIN}
  • %{SERV-ER_-NAME}
  • %{SERV-ER_-ADDR}
  • %{SERV-ER_-PORT}
  • %{SERV-ER_-PRO-TOCOL}
  • %{SERV-ER_-SOF-TWARE}

Special

  • %{API_-VER-SION}
  • %{THE_-REQ-UEST}
  • %{REQU-EST-_URI}
  • %{REQU-EST-_FI-LENAME}
  • %{IS_S-UBREQ}
  • %{HTTPS}

Request

  • %{REMO-TE_-ADDR}
  • %{REMO-TE_-HOST}
  • %{REMO-TE_-PORT}
  • %{REMO-TE_-USER}
  • %{REMO-TE_-IDENT}
  • %{REQU-EST-_ME-THOD}
  • %{SCRI-PT_-FIL-ENAME}
  • %{PATH-_INFO}
  • %{QUER-Y_S-TRING}
  • %{AUTH-_TYPE}

Time

  • %{TIME-_YEAR}
  • %{TIME-_MON}
  • %{TIME-_DAY}
  • %{TIME-_HOUR}
  • %{TIME-_MIN}
  • %{TIME-_SEC}
  • %{TIME-_WDAY}
  • %{TIME}

Leave a Reply

Your email address will not be published. Required fields are marked *