Apache Rewrite Cheatsheet

Introduction

Ok, so its not a sheet...its a blog post. Regardless, I often find myself on the hunt for Apache Rewrite flags and I've found a nice one at Ilovejackdaniels.

Before diving into the cheat sheet, its important to know how RewriteRules work (order of processing and whatnot). I've prettied up the image from Apache.org as pictures tell 1,000 words (click for a larger version):

rewrite

The order of rules in the ruleset is important because the rewriting engine processes them in a special (and not very obvious) order. The rule is this: The rewriting engine loops through the ruleset rule by rule (RewriteRule directives) and when a particular rule matches it optionally loops through existing corresponding conditions (RewriteCond directives). For historical reasons the conditions are given first, and so the control flow is a little bit long-winded. --Apache.org

Now that we've gotten that taken care of, lets move on to the cheat sheet. Ilovejackdaniels did the cheat sheet as a PDF and, frankly, I find PDFs annoying. So here's the contents:

Regular Expression Syntax

^ Start of string
$ End of string
. Any single character
(a|b) a or b
(...) Group section
[abc] Item in range (a or b or c)
[^abc] Not in range (not a or b or c)
a? Zero or one of a
a* Zero or more of a
a+ One or more of a
a{3} Exactly 3 of a
a{3,} 3 or more of a
a{3,6} Between 3 and 6 of a
!(pattern) "Not" prefix. Apply rule when URL does not match pattern

RewriteRule FLAGS

I'm going to go a little deeper in this list than what the cheat sheet does as I tend to need a little more of a description on what each flag does (all descriptions are lifted without remorse from Apache.org)...not just the short definition.

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.

F Forbidden (sends 403 header) This forces the current URL to be forbidden, i.e., it immediately sends back a HTTP response of 403 (FORBIDDEN). Use this flag in conjunction with appropriate RewriteConds to conditionally block some URLs.
G Gone (no longer exists) This forces the current URL to be gone, i.e., it immediately sends back a HTTP response of 410 (GONE). Use this flag to mark pages which no longer exist as gone.
P Proxy This flag forces the substitution part to be internally forced as a proxy request and immediately (i.e., rewriting rule processing stops here) put through the proxy module. You have to make sure that the substitution string is a valid URI (e.g., typically starting with http://hostname) which can be handled by the Apache proxy module. If not you get an error from the proxy module. Use this flag to achieve a more powerful implementation of the ProxyPass directive, to map some remote stuff into the namespace of the local server.

Notice: To use this functionality make sure you have the proxy module compiled into your Apache server program. If you don't know please check whether mod_proxy.c is part of the ``httpd -l'' output. If yes, this functionality is available to mod_rewrite. If not, then you first have to rebuild the ``httpd'' program with mod_proxy enabled.

L Last Rule Stop the rewriting process here and don't apply any more rewriting rules. This corresponds to the Perl last command or the break command from the C language. Use this flag to prevent the currently rewritten URL from being rewritten further by following rules. For example, use it to rewrite the root-path URL ('/') to a real one, e.g., '/e/www/'.
N Next (i.e. restart rules) Re-run the rewriting process (starting again with the first rewriting rule). Here the URL to match is again not the original URL but the URL from the last rewriting rule. This corresponds to the Perl next command or the continue command from the C language. Use this flag to restart the rewriting process, i.e., to immediately go to the top of the loop.

But be careful not to create an infinite loop!

C Chain This flag chains the current rule with the next rule (which itself can be chained with the following rule, etc.). This has the following effect: if a rule matches, then processing continues as usual, i.e., the flag has no effect. If the rule does not match, then all following chained rules are skipped. For instance, use it to remove the ``.www'' part inside a per-directory rule set when you let an external redirect happen (where the ``.www'' part should not to occur!).
T=mime-type Set Mime Type Force the MIME-type of the target file to be MIME-type. For instance, this can be used to simulate the mod_alias directive ScriptAlias which internally forces all files inside the mapped directory to have a MIME type of ``application/x-httpd-cgi''.
NS Skip if internal sub-request This flag forces the rewriting engine to skip a rewriting rule if the current request is an internal sub-request. For instance, sub-requests occur internally in Apache when mod_include tries to find out information about possible directory default files (index.xxx). On sub-requests it is not always useful and even sometimes causes a failure to if the complete set of rules are applied. Use this flag to exclude some rules.

Use the following rule for your decision: whenever you prefix some URLs with CGI-scripts to force them to be processed by the CGI-script, the chance is high that you will run into problems (or even overhead) on sub-requests. In these cases, use this flag.

NC Case insensitive This makes the Pattern case-insensitive, i.e., there is no difference between 'A-Z' and 'a-z' when Pattern is matched against the current URL.
QSA Append query string This flag forces the rewriting engine to append a query string part in the substitution string to the existing one instead of replacing it. Use this when you want to add more data to the query string via a rewrite rule.
NE Do not escape output This flag keeps mod_rewrite from applying the usual URI escaping rules to the result of a rewrite. Ordinarily, special characters (such as '%', '$', ';', and so on) will be escaped into their hexcode equivalents ('%25', '%24', and '%3B', respectively); this flag prevents this from being done. This allows percent symbols to appear in the output, as in

RewriteRule /foo/(.*) /bar?arg=P1\%3d$1 [R,NE]

which would turn '/foo/zed' into a safe request for '/bar?arg=P1=zed'.

PT Pass through This flag forces the rewriting engine to set the uri field of the internal request_rec structure to the value of the filename field. This flag is just a hack to be able to post-process the output of RewriteRule directives by Alias, ScriptAlias, Redirect, etc. directives from other URI-to-filename translators. A trivial example to show the semantics: If you want to rewrite /abc to /def via the rewriting engine of mod_rewrite and then /def to /ghi with mod_alias:

RewriteRule ^/abc(.*) /def$1 [PT]
Alias /def /ghi

If you omit the PT flag then mod_rewrite will do its job fine, i.e., it rewrites uri=/abc/... to filename=/def/... as a full API-compliant URI-to-filename translator should do. Then mod_alias comes and tries to do a URI-to-filename transition which will not work.

Note: You have to use this flag if you want to intermix directives of different modules which contain URL-to-filename translators. The typical example is the use of mod_alias and mod_rewrite..

S=x Skip next x rules This flag forces the rewriting engine to skip the next num rules in sequence when the current rule matches. Use this to make pseudo if-then-else constructs: The last rule of the then-clause becomes skip=N where N is the number of rules in the else-clause. (This is not the same as the 'chain|C' flag!)
E=var:value Set environment variable "var" to "value" This forces an environment variable named VAR to be set to the value VAL, where VAL can contain regexp backreferences $N and %N which will be expanded. You can use this flag more than once to set more than one variable. The variables can be later dereferenced in many situations, but usually from within XSSI (via ) or CGI (e.g. $ENV{'VAR'}). Additionally you can dereference it in a following RewriteCond pattern via %{ENV:VAR}. Use this to strip but remember information from URLs.

RewriteCond FLAGS

NC Case insensitive
OR Allows a rule to apply if one of a series of conditions are true.

Redirection Header Codes

301 Moved permanently
302 Moved temporarily
403 Forbidden
404 Not found
410 Gone

Server Variables

Format
  %{NAME_OF_VAR}
HTTP Headers
  HTTP_USER_AGENT
  HTTP_REFERER
  HTTP_COOKIE
  HTTP_FORWARDED
  HTTP_HOST
  HTTP_PROXY_CONNECTION
  HTTP_ACCEPT
Request
  REMOTE_ADDR
  REMOTE_HOST
  REMOTE_USER
  REMOTE_IDENT
  REQUEST_METHOD
  SCRIPT_FILENAME
  PATH_INFO
  QUERY_STRING
  AUTH_TYPE
Server
  DOCUMENT_ROOT
  SERVER_ADMIN
  SERVER_NAME
  SERVER_ADDR
  SERVER_PORT
  SERVER_PROTOCOL
  SERVER_SOFTWARE
Time
  TIME_YEAR
  TIME_MON
  TIME_DAY
  TIME_HOUR
  TIME_MIN
  TIME_SEC
  TIME_WDAY
  TIME
Special
  API_VERSION
  THE_REQUEST
  REQUEST_URI
  REQUEST_FILENAME
  IS_SUBREQ

Directives

  RewriteEngine
  RewriteOptions
  RewriteLog
  RewriteLogLevel
  RewriteLock
  RewriteMap
  RewriteBase
  RewriteCond
  RewriteRule

Example Rules

CODE:
  1. # Site has permanently moved to new domain
  2. # domain.com to domain2.com
  3. RewriteCond   %{HTTP_HOST}   ^www.domain.com$   [NC]
  4. RewriteRule   ^(.*)$   http://www.domain2.com/$1   [R=301,L]

CODE:
  1. # Page has moved temporarily
  2. # domain.com/page.html to domain.com/new_page.html
  3. RewriteRule   ^page.html$   new_page.html   [R,NC,L]

CODE:
  1. # Nice looking URLs (no query string)
  2. # domain.com/category-name-1/ to domain.com/categories.php?name=category-name-1
  3. RewriteRule   ^([A-Za-z0-9-]+)/?$   categories.php?name=$1 [L]

CODE:
  1. # Nice looking URLs (no query string) with pagination
  2. # domain.com/articles/title/5 to domain.com/article.php?name=title&page=5
  3. RewriteRule   ^articles/([A-Za-z0-9-]+)/([0-9]+)/?$   article.php?name=$1&page=$2 [L]

CODE:
  1. # Block referrer spam
  2. RewriteCond  %{HTTP_REFERRER}  (weight)  [NC,OR]
  3. RewriteCond  %{HTTP_REFERRER}  (drugs)  [NC]
  4. RewriteRule  .*  -  [F]

In addition to the above, the Apache Rewrite Guide may prove useful!

Discuss This Article


15 Responses to “Apache Rewrite Cheatsheet”

  1. AvatarG

    1.# Nice looking URLs (no query string) with pagination
    2.# domain.com/articles/title/5 to domain.com/article.php?name=title&page=5
    3.RewriteRule ^articles/([A-Za-z0-9-]+)/([0-9]+)/?$ article.php?name=$1&page=$2 [L]

    Just what I was looking for, but seems to be exactly the wrong way round?
    i.e. it is taking the clean format and turning it into the query format…
    or is my Apache stupid?

    Reply to this comment.
    1
  2. AvatarMatt
    Author Comment

    @G

    Well, it shouldn’t be redirecting to an unfriendly URL…The URL should still look pretty while masking the “unfriendly call.” Hrm.

    Reply to this comment.
    2
  3. pingback pingback:
    Feeds On Rails

    [...] into a new directory within your rails ‘public’ folder. Once that is done, we need to coax apache into recognizing the difference between Rails and PHP [...]

    Reply to this comment.
    3
  4. pingback pingback:
    links for 2007-10-18 « B-link List

    [...] Apache Rewrite Cheatsheet Ok, so its not a sheet…its a blog post. Regardless, I often find myself on the hunt for Apache Rewrite flags and I’ve found a nice one at Ilovejackdaniels. (tags: apache rewrite cheatsheet mod_rewrite reference htaccess url) [...]

    Reply to this comment.
    4
  5. pingback pingback:
    Integrating Wordpress and Rails - igvita.com

    [...] files into a new directory within your rails ‘public’ folder. Once that is done, we need to coax apache into recognizing the difference between Rails and PHP [...]

    Reply to this comment.
    5
  6. AvatarThuc NGuyen

    I have some problem with css, js, image file when I rewrite url
    EX: example.com/article.php?title=title$author=name
    ->example.com/article/title/name

    css, js, or image will example.com/article/title/css….

    Can you help me! Thanks

    Reply to this comment.
    6
  7. AvatarIndiX

    @ #6

    Try with absolute paths, or just place a base href in your header. That should work.

    Reply to this comment.
    7
  8. Avatargeorgezzz

    @G -
    remove the [L] flag and it’ll look friendly.

    1.# Nice looking URLs (no query string) with pagination
    2.# domain.com/articles/title/5 to domain.com/article.php?name=title&page=5
    3.RewriteRule ^articles/([A-Za-z0-9-]+)/([0-9]+)/?$ article.php?name=$1&page=$2 [L]
    Just what I was looking for, but seems to be exactly the wrong way round?
    i.e. it is taking the clean format and turning it into the query format…
    or is my Apache stupid?

    Reply to this comment.
    8
  9. Avatarbounce

    @georgezzz … the example is a recommendation on best practice given the situation it describes.. It is recommended that URL’s these days are kept as clean, RESTful / meaningful, and hackable as possible… Formatting URLS this way and then rewriting them to query string params is a lot more friendly to a sites audience then going at it the other way round.

    Reply to this comment.
    9
  10. AvatarTony

    Hello,

    I’ve been at it an hour and am stumped. I must be missing something small. I’m trying to do this:

    domain.com/browse/News/Media/ to domain.com/index.php?browse=/News/Media/

    and I came up with this:
    RewriteRule ^browse/([A-Za-z0-9-]+)/?$ index.php?browse=$1

    Any ideas?

    Thanks.
    -Tony

    Reply to this comment.
    10
  11. AvatarShikha

    I have following rewrite rule which works perfectly fine in FireFox but not in IE. Any idea?

    RewriteCond %{HTTP:Content-Type} multipart/form-data
    RewriteCond %{REQUEST_URI} ^/upload
    RewriteCond %{REQUEST_METHOD} POST
    RewriteRule ^/(.*) http://127.0.0.1:5000/1 [P]

    Reply to this comment.
    11
  12. AvatarManolo
    12
  13. AvatarHelper

    Hello,
    I’ve been at it an hour and am stumped. I must be missing something small. I’m trying to do this:
    domain.com/browse/News/Media/ to domain.com/index.php?browse=/News/Media/
    and I came up with this:
    RewriteRule ^browse/([A-Za-z0-9-]+)/?$ index.php?browse=$1
    Any ideas?
    Thanks.
    -Tony

    How about:

    RewriteRule ^browse(.*) index.php?browse=$1

    Reply to this comment.
    13
  14. AvatarHelper
    Hello,
    I’ve been at it an hour and am stumped. I must be missing something small. I’m trying to do this:
    domain.com/browse/News/Media/ to domain.com/index.php?browse=/News/Media/
    and I came up with this:
    RewriteRule ^browse/([A-Za-z0-9-]+)/?$ index.php?browse=$1
    Any ideas?
    Thanks.
    -Tony

    How about:
    RewriteRule ^browse(.*) index.php?browse=$1

    RewriteRule ^/+browse(.*) index.php?browse=$1

    Reply to this comment.
    14
  15. AvatarJohn

    Hey Everyone!!
    I’m really, really new to Apache and am hoping someone knows the answer to this one. Ok, I’ve got a domain, we’ll call it domain.com. I have a new domain, we’ll call it: newdomain.com. If a user types in the domain.com or http://www.domain.com it works fine, the problem is if a user has domain.com/pages/contact.html bookmarked, it takes them to an Error 404 page. How do I get rid of all the stuff past the / ?

    RewriteEngine On
    RewriteCond %{HTTP_HOST} domain.com [OR]
    RewriteCond %{HTTP_HOST} http://www.domain.com
    RewriteRule ^(.*)$ http://www.newdomain.com/new_dir1 [R=301,L,QSA]

    TIA!!!

    Reply to this comment.
    15

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Comment Preview:

 (9346) - apache rewrite (541) - apache rewriterule (270) - apache redirect (165) - apache rewrite rules (68) - rewriterule apache (60) - apache rewrite examples (43) - apache rewrite rule (28) - RewriteRule forbidden (19) - apache rewrites (17) - apache rewrite 404 (16) - apache rewrite flags (15) - apache rewrite NC (15) - Proxypass rewrite (15) - mod rewrite cheat sheet (15) - apache rewrite hostname (14) - apache (13) - apache rewrite www (13) - rewrite apache (13) - apache url rewrite (13) - apache rewrite condition (12) - apache mod_rewrite (11) - apache rewrite query string (10) - apache rewrite url (10) - apache rewrite example (10) - apache redirect root (10) - apache rewrite syntax (10) - rewrite root (10) - apache rewrite alias (8) - apache rewriterule flags (8) - apache rewrite PT (7) - apache redirect loop (7) - apache rewrite remove www (7) - apache rewrite QUERY_STRING (7) - apache RewriteRule example (7) - Apache Rewrite proxy (6) - apache rewrite post (6) - apache rewrite case (6) - apache rewrite rule syntax (6) - rewrite exclude (6) - apache rewrite root (6) - apache RewriteRule examples (6) - apache redirect examples (6) - apache rewrite 403 (6) - apache HTTP_COOKIE (6) - apache alias rewrite (6) - apache rewrite response (6) - apache rewrite rule example (6) - apache rewrite [NC] (6) - APACHE redirect syntax (5) - apache rewriterule pt (5) - apache proxy rewrite (5) - rewrite referrer (5) - rewrite Cheat Sheet (5) - rewriterule exclude directory (5) - apache mod_rewrite proxy (5) - apache rewrite forbidden (5) - apache redirect rules (5) - apache RewriteRule Proxy (5) - apache rewriterule 404 (5) - apache redirect referer (5) - apache rewrite variable (5) - apache request_method (5) - apache rewrite cheat sheet (5) - rewrite rule forbidden (5) - rewrite alias (5) - apache rewrite http headers (5) - apache query_string (5) - apache 2 rewrite rules (5) - apache rewrite directory (4) - apache rewrite domain (4) - apache rewriterule case insensitive (4) - RewriteCond querystring (4) - apache rewrite REQUEST_METHOD (4) - mod_rewrite exclude directory (4) - rewrite 403 (4) - apache rewrite cond (4) - apache rewrite http_host (4) - Rewrite variables (4) - apache redirect 403 (4) - apache rewrite cheatsheet (4) - apache rewrite request (4) - apache header rewriting (4) - apache RewriteRule query_string (4) - Apache Redirect config (4) - apache force redirect (4) - apache rewrite environment variable (4) - htaccess rewrite proxy (4) - Apache RewriteRule NC (4) - apache rewrite / (4) - rewrite condition apache (4) - rewrite get variables (4) - apache2 rewrite (4) - apache rewritecond (4) - apache redirect root to a url (4) - rewrite url variable php apache (4) - apache RewriteRule help (4) - apache 4 redirect through apache (4) - apache rewrite variables (3) - apache rewriterule syntax (3) - apache rewrite cgi (3) - apache rewrite header (3) - apache header rewrite (3) - apache rewrite conditions (3) - apache redirect hostname (3) - apache redirect exclude (3) - apache rewrite query (3) - apache rewrite language (3) - apache 404 rewrite (3) - htaccess rewrite exclude (3) - rewrite forbidden (3) - rewrite syntax (3) - apache redirect nc (3) - apache redirect case insensitive (3) - apache rewriting (3) - apache rewrite url 404 (3) - apache redirect www (3) - apache rewrite flag (3) - apache rewrite HTTP header (3) - apache rewrite rule 404 (3) - apache rewrite redirect loop (3) - apache QUERY_STRING RewriteCond (3) - apache rewrite post request (3) - apache hostname rewrite (3) - apache rewriterules (3) - apache rewrite to www (3) - apache mod_rewrite skip (3) - apache rewrite site moved (3) - Apache RewriteRule [PT] (3) - apache HTTP_FORWARDED (3) - apache rewrite remove string from url (3) - rewrite proxy apache (3) - apache mod_rewrite example restful (3) - rewrite header apache (3) - apache rewrite module (3) - apache infinite rewrite loop (3) - rewritemap apache example (3) - mod rewrite images gone (3) - apache url rewrite examples (3) - apache2 rewrite rule (3) - rewrite PT (3) - rewrite apache root (3) - apache url rewrite flags (3) - rewriterule apache wrong path (3) - apache query variable rewrite (3) - apache module rewrite (3) - apache redirect query string (2) - apache domain rewrite (2) - apache rewrite referer (2) - apache rewrite 302 (2) -