mod_rewrite.c revision dd01540ada755829d0138f1ca7f4f7094e9154f4
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth/* ====================================================================
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * The Apache Software License, Version 1.1
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * Copyright (c) 2000-2003 The Apache Software Foundation. All rights
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * reserved.
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * Redistribution and use in source and binary forms, with or without
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * modification, are permitted provided that the following conditions
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * are met:
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * 1. Redistributions of source code must retain the above copyright
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * notice, this list of conditions and the following disclaimer.
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * 2. Redistributions in binary form must reproduce the above copyright
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * notice, this list of conditions and the following disclaimer in
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * the documentation and/or other materials provided with the
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * distribution.
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * 3. The end-user documentation included with the redistribution,
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * if any, must include the following acknowledgment:
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * "This product includes software developed by the
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * Apache Software Foundation (http://www.apache.org/)."
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * Alternately, this acknowledgment may appear in the software itself,
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * if and wherever such third-party acknowledgments normally appear.
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * 4. The names "Apache" and "Apache Software Foundation" must
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * not be used to endorse or promote products derived from this
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * software without prior written permission. For written
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * permission, please contact apache@apache.org.
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * 5. Products derived from this software may not be called "Apache",
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * nor may "Apache" appear in their name, without prior written
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * permission of the Apache Software Foundation.
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * SUCH DAMAGE.
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * ====================================================================
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * This software consists of voluntary contributions made by many
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * individuals on behalf of the Apache Software Foundation. For more
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * information on the Apache Software Foundation, please see
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * Portions of this software are based upon public domain software
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * originally written at the National Center for Supercomputing Applications,
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth * University of Illinois, Urbana-Champaign.
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth** _ __ ___ ___ __| | _ __ _____ ___ __(_) |_ ___
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth** | '_ ` _ \ / _ \ / _` | | '__/ _ \ \ /\ / / '__| | __/ _ \
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth** | | | | | | (_) | (_| | | | | __/\ V V /| | | | || __/
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth** |_| |_| |_|\___/ \__,_|___|_| \___| \_/\_/ |_| |_|\__\___|
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth** URL Rewriting Module
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth** This module uses a rule-based rewriting engine (based on a
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth** regular-expression parser) to rewrite requested URLs on the fly.
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth** It supports an unlimited number of additional rule conditions (which can
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth** operate on a lot of variables, even on HTTP headers) for granular
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth** matching and even external database lookups (either via plain text
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth** tables, DBM hash files or even external processes) for advanced URL
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth** substitution.
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth** It operates on the full URLs (including the PATH_INFO part) both in
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth** per-server context (httpd.conf) and per-dir context (.htaccess) and even
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth** can generate QUERY_STRING parts on result. The rewriting result finally
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth** can lead to internal subprocessing, external request redirection or even
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth** to internal proxy throughput.
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth** This module was originally written in April 1996 and
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth** gifted exclusively to the The Apache Software Foundation in July 1997 by
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth** Ralf S. Engelschall
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth** rse@engelschall.com
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth#if !defined(OS2) && !defined(WIN32) && !defined(BEOS) && !defined(NETWARE)
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth#define MOD_REWRITE_SET_MUTEX_PERMS /* XXX Apache should define something */
b1dd958f54f8bfa984d306bb8ca8264855761d7bcth** +-------------------------------------------------------+
static int proxy_available;
static const char *lockname;
a->rewriteloglevel = 0;
a->server = s;
sizeof(rewrite_server_conf));
sizeof(rewrite_perdir_conf));
return NULL;
while (*option) {
if (limit <= 0) {
return NULL;
return NULL;
const char *a1)
return NULL;
const char *a2)
const char *ignored_fname;
int bad = 0;
if (colon) {
++bad;
++bad;
if (bad) {
return NULL;
const char *error;
return error;
if (!lockname) {
return NULL;
const char *a1)
return NULL;
const char *in_str)
char *a1;
char *a2;
char *a3;
char *cp;
const char *err;
int rc;
return err;
cp++;
== NULL);
if (rc) {
return NULL;
char *str)
char *cp;
char *cp1;
char *cp2;
char *cp3;
char *key;
char *val;
const char *err;
return err;
return NULL;
return NULL;
const char *in_str)
char *a1;
char *a2;
char *a3;
char *cp;
const char *err;
int mode;
return err;
cp++;
sizeof(rewritecond_entry));
sizeof(rewritecond_entry));
return NULL;
char *str)
char *cp;
char *cp1;
char *cp2;
char *cp3;
char *key;
char *val;
const char *err;
return err;
return NULL;
int status = 0;
if (i < MAX_ENV_FLAGS) {
if (i < MAX_COOKIE_FLAGS) {
return NULL;
if (map_pfn_register) {
return OK;
server_rec *s)
void *data;
int first_time = 0;
if (!data) {
return HTTP_INTERNAL_SERVER_ERROR;
#ifdef MOD_REWRITE_SET_MUTEX_PERMS
return HTTP_INTERNAL_SERVER_ERROR;
return HTTP_INTERNAL_SERVER_ERROR;
for (; s; s = s->next) {
open_rewritelog(s, p);
if (!first_time) {
return HTTP_INTERNAL_SERVER_ERROR;
return OK;
lockname, p);
const char *saved_rulestatus;
const char *var;
const char *thisserver;
char *thisport;
const char *thisurl;
const char *ccp;
unsigned int port;
int rulestatus;
return DECLINED;
return DECLINED;
r->filename);
if (rulestatus) {
unsigned skip;
if (!proxy_available) {
return HTTP_FORBIDDEN;
r->filename);
return OK;
if (r->args) {
? r->args
NULL);
n = r->status;
return HTTP_FORBIDDEN;
return HTTP_GONE;
return DECLINED;
#if APR_HAS_USER
return HTTP_BAD_REQUEST;
(r->filename +
r->filename);
return OK;
return DECLINED;
if (t == NULL) {
return DECLINED;
r->filename, t);
ap_set_content_type(r, t);
return OK;
char *cp;
char *cp2;
const char *ccp;
char *prefix;
int rulestatus;
char *ofilename;
return DECLINED;
return DECLINED;
return DECLINED;
* URL: http://localhost/foo and .htaccess is located in foo directory
if (r->filename &&
return DECLINED;
return DECLINED;
return HTTP_FORBIDDEN;
if (rulestatus) {
unsigned skip;
return OK;
* hostname and compare/substitute only the stuff after it.
if (r->args) {
? r->args
NULL);
n = r->status;
return HTTP_FORBIDDEN;
return HTTP_GONE;
return HTTP_BAD_REQUEST;
return OK;
r->filename+l);
return OK;
return DECLINED;
return DECLINED;
return DECLINED;
if (is_redirect_limit_exceeded(r)) {
return HTTP_INTERNAL_SERVER_ERROR;
return OK;
if (!reqc) {
char *perdir)
int changed;
int rc;
changed = 0;
loop:
p = &entries[i];
if (rc) {
goto loop;
if (p->skip > 0) {
s = p->skip;
p = &entries[i];
p = &entries[i];
return changed;
char *perdir)
char *uri;
char *output;
const char *vary;
int prefixstrip;
int failed;
int rc;
prefixstrip = 0;
sizeof(regmatch));
failed = 0;
c = &conds[i];
if (rc == 0) {
c = &conds[i];
if (rc == 0) {
if (failed) {
p->forced_mimetype);
reduce_uri(r);
r->filename);
p->forced_mimetype);
int rc;
rc = 0;
#if !defined(OS2)
sizeof(regmatch));
return rc;
inp++;
goto skip;
char *endp;
goto skip;
goto skip;
if (result) {
char *var;
span = 0;
skip:
space--;
char *olduri;
if (q != NULL) {
if (qsappend) {
r->filename);
char *cp;
apr_size_t l;
unsigned short port;
++cp;
++cp;
if (!*url) {
const char *thisserver;
char *thisport;
int port;
switch (*uri++) {
char *cp;
return NULL;
++cp;
++cp;
NULL);
#if APR_HAS_USER
char *newuri;
char *homedir;
return newuri;
rewritemap_entry *s;
char *value;
s = &entries[i];
return NULL;
if ((value =
return value;
return NULL;
return NULL;
if ((value =
return value;
return NULL;
if ((value =
return value;
return value;
return NULL;
if ((value =
return NULL;
value);
return value;
return NULL;
char *cpT;
char *curkey;
char *curval;
return NULL;
if (skip == 0) {
++cpT;
if (skip == 0) {
return value;
return value;
#ifndef NO_WRITEV
return NULL;
if (rewrite_mapr_lock_acquire) {
#ifdef NO_WRITEV
buf[i++] = c;
if (rewrite_mapr_lock_acquire) {
return NULL;
cp++) {
return value;
cp++) {
return value;
char *value;
return value;
char *value;
return value;
static int rewrite_rand_init_done = 0;
static void rewrite_rand_init(void)
if (!rewrite_rand_init_done) {
static int rewrite_rand(int l, int h)
char *buf;
return value;
return buf;
const char *fname;
if (!fname) {
!= APR_SUCCESS) {
char *str1;
const char *type;
char *ruser;
const char *rhost;
(unsigned long)(r->server), (unsigned long)r,
return APR_SUCCESS;
APR_LOCK_DEFAULT, p);
return rc;
#ifdef MOD_REWRITE_SET_MUTEX_PERMS
return rc;
return APR_SUCCESS;
return APR_SUCCESS;
return APR_SUCCESS;
return rc;
return APR_SUCCESS;
const char *desc)
!= APR_SUCCESS) ||
procattr, p);
if (fpin) {
if (fpout) {
return (rc);
const char *result;
/* XXX: wow this has gotta be slow if you actually use it for a lot, recalculates exploded time for each variable */
return (char *)result; \
return NULL;
cache *c;
return NULL;
#if APR_HAS_THREADS
return NULL;
return NULL;
return NULL;
return n % CACHE_TLB_ROWS;
char *key)
for (i=0; i < CACHE_TLB_COLS; ++i) {
return NULL;
return &elt[j];
return NULL;
cacheentry *e)
cachelist *l;
cacheentry *e;
cachetlbentry *t;
int found_list;
#if APR_HAS_THREADS
found_list = 0;
if (e != NULL) {
#if APR_HAS_THREADS
#if APR_HAS_THREADS
if (!found_list) {
sizeof(cachetlbentry));
for (i=0; i<CACHE_TLB_ROWS; ++i) {
for (j=0; j<CACHE_TLB_COLS; ++j)
#if APR_HAS_THREADS
#if APR_HAS_THREADS
cachelist *l;
cacheentry *e;
#if APR_HAS_THREADS
if (e != NULL) {
#if APR_HAS_THREADS
#if APR_HAS_THREADS
#if APR_HAS_THREADS
return NULL;
const char *subst)
char *output;
return output;
char *cp;
int isquoted;
cp++; \
isquoted = 0; \
cp++; \
cp++; \
char *cp;
char *var;
char *val;
char *domain;
char *expires;
char *path;
char *tok_cntx;
char *cookie;
if (expires) {
char *notename;
void *data;
(expires)?
r->request_time +
: NULL,
NULL);
const char *root;
const char *slash;
char *statpath;
for (i = 0; i < n1; i++) {
int depth;
else if (*s == left) {
++depth;
return NULL;
int depth;
return NULL;
else if (*s == left) {
++depth;
return NULL;
{ NULL }