This document provides examples and explanations of mod_rewrite syntax and flags for redirecting URLs and blocking referrer spam. Regular expressions are used to match patterns in URLs and referrers. RewriteRule directives specify the matching patterns and replacement URLs, and RewriteCond directives set conditions that must be met. Flags like R, L, and NC control how redirects are handled.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0 ratings0% found this document useful (0 votes)
113 views1 page
MOD - Rewrite Cheat Sheet v2
This document provides examples and explanations of mod_rewrite syntax and flags for redirecting URLs and blocking referrer spam. Regular expressions are used to match patterns in URLs and referrers. RewriteRule directives specify the matching patterns and replacement URLs, and RewriteCond directives set conditions that must be met. Flags like R, L, and NC control how redirects are handled.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 1
mod_rewrite
Regular Expressions Syntax Example: New domain
^ Start of string #domain.com to domain2.com
$ End of string RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC] . Any single character RewriteRule ^(.*)$ http://www.domain2.com/$1 [R=301,L] (a|b) a or b (...) Group section Example: Page moved temporarily [abc] Item in range (a, b or c) [^abc] Not in range (not a, b or c) # domain.com/page.htm to domain.com/new_page.htm \s White space RewriteRule ^page.htm$ new_page.htm [R,NC,L] a? Zero or one of a a* Zero or more of a Example: Nice looking URLs (no querystring) a*? Zero or more of a, ungreedy a+ One or more of a # domain.com/category-name/ to domain.com/categories.php?name=category-name a+? One or more of a, ungreedy RewriteRule ^([A-Za-z0-9-]+)/?$ categories.php?name=$1 [L] a{3} Exactly 3 of a a{3,} 3 or more of a Example: Block Referrer Spam (where referring URL contains "viagra" or "xxx" ) a{3,6} 3 to 6 of a a{3,6}? 3 to 6 of a, ungreedy RewriteCond %{HTTP_REFERER} (viagra) [NC,OR] !(pattern) "Not" prefix. Apply rule if RewriteCond %{HTTP_REFERER} (xxx) [NC] pattern does not match. RewriteRule .* - [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} F Forbidden (sends 403 %{HTTP_COOKIE} %{TIME_DAY} header) %{HTTP_FORWARDED} %{TIME_HOUR} G Gone (no longer exists) %{HTTP_HOST} %{TIME_MIN} P Proxy %{HTTP_PROXY_CONNECTION} %{TIME_SEC} L Last Rule %{HTTP_ACCEPT} %{TIME_WDAY} N Next (i.e. restart rules) %{TIME} C Chain Server Variables: Request T=mime-type Set Mime Type Server Variables: Special 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} Directives %{AUTH_TYPE} RewriteEngine RewriteCond Flags RewriteOptions Server Variables: HTTP Headers NC Case insensitive RewriteLog OR Combines rules with logical %{DOCUMENT_ROOT} RewriteLogLevel "or" instead of "and". %{SERVER_ADMIN} RewriteLock %{SERVER_NAME} RewriteMap %{SERVER_ADDR} RewriteBase Redirection Header Codes %{SERVER_PORT} RewriteCond 301 Moved permanently %{SERVER_PROTOCOL} RewriteRule 302 Moved temporarily %{SERVER_SOFTWARE} 403 Forbidden Available free from 404 Not Found AddedBytes.com 410 Gone