Mod rewrite Cheat Sheet

Explore key concepts, syntax, and usage examples for Mod rewrite — perfect for quick reference.

Resource
Related
RewriteRule Flags
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
Miscellaneous
RewriteCond Flags
  • NC
    💡 Case insensitive
  • OR
    💡 Combine with next rule using 'OR' instead of the default of 'AND'
Redirection Header Codes
  • 301
    💡 Moved permanently
  • 302
    💡 Moved temporarily (default)
Sample Rule: Site Moved
  • 💡 # Site moved permanently
  • RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
  • RewriteRule ^(.*)$ http://www.domain2.com/$1 [R=301,L]
    💡 Rewrites domain.com to domain2.com
Sample Rule: Temporary Page Move
  • 💡 # Page has moved temporarily
  • RewriteRule ^page.html$ new_page.html [R,NC,L]
    💡 Rewrites domain.com/page.html to domain.com/new_page.html
Sample Rule: Nice URLs
  • 💡 # Nice URLs (no query string)
  • RewriteRule ^([A-Za-z0-9]+)/?$ categories.php?name=$1 [L]
    💡 Rewrites domain.com/categoryname1/ to domain.com/categories.php?name=category-name1
HTTP Headers
Server Internals
Special
Request
Time