Regular Expression Syntax
RewriteRule FLAGS
R[=code]
Redirect to new URL, with optional code (see below).
Prefix Substitution with http://thishost[:thisport]/ (which makes the new URL a URI) to force a external redirection. If no code is given a HTTP response of 302 (MOVED TEMPORARILY) is used. If you want to use other response codes in the range 300-400 just specify them as a number or use one of the following symbolic names: temp (default), permanent, seeother. Use it for rules which should canonicalize the URL and give it back to the client, e.g., translate ``/~'' into ``/u/'' or always append a slash to /u/user, etc.
Note: When you use this flag, make sure that the substitution field is a valid URL! If not, you are redirecting to an invalid location! And remember that this flag itself only prefixes the URL with http://thishost[:thisport]/, rewriting continues. Usually you also want to stop and do the redirection immediately. To stop the rewriting you also have to provide the 'L' flag.
RewriteCond FLAGS
NC
OR
Case insensitive
Allows a rule to apply if one of a series of conditions are true
Code Example
Site permanently moved to new domain
domain.com to domain2.com
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [R=301,L]
Page has moved temporarily
domain.com/page.html to domain.com/new_page.html
RewriteRule ^page.html$ new_page.html [R,NC,L]
Nice looking URLs (no query string)
domain.com/category-name-1/ to domain.com/categories.php?name=category-name-1
RewriteRule ^([A-Za-z0-9-]+)/?$ categories.php?name=$1 [L]
Nice looking URLs (no query string) with pagination
domain.com/articles/title/5 to domain.com/article.php?name=title&page=5
RewriteRule ^articles/([A-Za-z0-9-]+)/([0-9]+)/?$ article.php?name=$1&page=$2 [L]
Block referrer spam
RewriteCond %{HTTP_REFERRER} (weight) [NC,OR]
RewriteCond %{HTTP_REFERRER} (drugs) [NC]
RewriteRule .* - [F]