📄 mod_rewrite.c
字号:
/* ==================================================================== * Copyright (c) 1996-1998 The Apache Group. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. All advertising materials mentioning features or use of this * software must display the following acknowledgment: * "This product includes software developed by the Apache Group * for use in the Apache HTTP server project (http://www.apache.org/)." * * 4. The names "Apache Server" and "Apache Group" must not be used to * endorse or promote products derived from this software without * prior written permission. For written permission, please contact * apache@apache.org. * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * 6. Redistributions of any form whatsoever must retain the following * acknowledgment: * "This product includes software developed by the Apache Group * for use in the Apache HTTP server project (http://www.apache.org/)." * * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Group and was originally based * on public domain software written at the National Center for * Supercomputing Applications, University of Illinois, Urbana-Champaign. * For more information on the Apache Group and the Apache HTTP server * project, please see <http://www.apache.org/>. * *//* _ _ _** _ __ ___ ___ __| | _ __ _____ ___ __(_) |_ ___** | '_ ` _ \ / _ \ / _` | | '__/ _ \ \ /\ / / '__| | __/ _ \** | | | | | | (_) | (_| | | | | __/\ V V /| | | | || __/** |_| |_| |_|\___/ \__,_|___|_| \___| \_/\_/ |_| |_|\__\___|** |_____|**** URL Rewriting Module**** This module uses a rule-based rewriting engine (based on a** regular-expression parser) to rewrite requested URLs on the fly.**** It supports an unlimited number of additional rule conditions (which can** operate on a lot of variables, even on HTTP headers) for granular** matching and even external database lookups (either via plain text** tables, DBM hash files or even external processes) for advanced URL** substitution.**** It operates on the full URLs (including the PATH_INFO part) both in** per-server context (httpd.conf) and per-dir context (.htaccess) and even** can generate QUERY_STRING parts on result. The rewriting result finally** can lead to internal subprocessing, external request redirection or even** to internal proxy throughput.**** This module was originally written in April 1996 and** gifted exclusively to the The Apache Group in July 1997 by**** Ralf S. Engelschall** rse@engelschall.com** www.engelschall.com*/#include "mod_rewrite.h"/*** +-------------------------------------------------------+** | |** | static module configuration** | |** +-------------------------------------------------------+*//*** Our interface to the Apache server kernel:**** o Runtime logic of a request is as following:** while(request or subrequest)** foreach(stage #0...#9)** foreach(module) (**)** try to run hook**** o the order of modules at (**) is the inverted order as** given in the "Configuration" file, i.e. the last module** specified is the first one called for each hook!** The core module is always the last!**** o there are two different types of result checking and** continue processing:** for hook #0,#1,#4,#5,#6,#8:** hook run loop stops on first modules which gives** back a result != DECLINED, i.e. it usually returns OK** which says "OK, module has handled this _stage_" and for #1** this have not to mean "Ok, the filename is now valid".** for hook #2,#3,#7,#9:** all hooks are run, independend of result**** o at the last stage, the core module always** - says "BAD_REQUEST" if r->filename does not begin with "/"** - prefix URL with document_root or replaced server_root** with document_root and sets r->filename** - always return a "OK" independed if the file really exists** or not!*/ /* The section for the Configure script: * MODULE-DEFINITION-START * Name: rewrite_module * ConfigStart . ./helpers/find-dbm-lib if [ "x$found_dbm" = "x1" ]; then echo " enabling DBM support for mod_rewrite" else echo " disabling DBM support for mod_rewrite" echo " (perhaps you need to add -ldbm, -lndbm or -lgdbm to EXTRA_LIBS)" CFLAGS="$CFLAGS -DNO_DBM_REWRITEMAP" fi * ConfigEnd * MODULE-DEFINITION-END */ /* the table of commands we provide */static const command_rec command_table[] = { { "RewriteEngine", cmd_rewriteengine, NULL, OR_FILEINFO, FLAG, "On or Off to enable or disable (default) the whole rewriting engine" }, { "RewriteOptions", cmd_rewriteoptions, NULL, OR_FILEINFO, ITERATE, "List of option strings to set" }, { "RewriteBase", cmd_rewritebase, NULL, OR_FILEINFO, TAKE1, "the base URL of the per-directory context" }, { "RewriteCond", cmd_rewritecond, NULL, OR_FILEINFO, RAW_ARGS, "a input string and a to be applied regexp-pattern" }, { "RewriteRule", cmd_rewriterule, NULL, OR_FILEINFO, RAW_ARGS, "a URL-applied regexp-pattern and a substitution URL" }, { "RewriteMap", cmd_rewritemap, NULL, RSRC_CONF, TAKE2, "a mapname and a filename" }, { "RewriteLock", cmd_rewritelock, NULL, RSRC_CONF, TAKE1, "the filename of a lockfile used for inter-process synchronization"}, { "RewriteLog", cmd_rewritelog, NULL, RSRC_CONF, TAKE1, "the filename of the rewriting logfile" }, { "RewriteLogLevel", cmd_rewriteloglevel, NULL, RSRC_CONF, TAKE1, "the level of the rewriting logfile verbosity " "(0=none, 1=std, .., 9=max)" }, { NULL }}; /* the table of content handlers we provide */static const handler_rec handler_table[] = { { "redirect-handler", handler_redirect }, { NULL }}; /* the main config structure */module MODULE_VAR_EXPORT rewrite_module = { STANDARD_MODULE_STUFF, init_module, /* module initializer */ config_perdir_create, /* create per-dir config structures */ config_perdir_merge, /* merge per-dir config structures */ config_server_create, /* create per-server config structures */ config_server_merge, /* merge per-server config structures */ command_table, /* table of config file commands */ handler_table, /* [#8] MIME-typed-dispatched handlers */ hook_uri2file, /* [#1] URI to filename translation */ NULL, /* [#4] validate user id from request */ NULL, /* [#5] check if the user is ok _here_ */ NULL, /* [#2] check access by host address */ hook_mimetype, /* [#6] determine MIME type */ hook_fixup, /* [#7] pre-run fixups */ NULL, /* [#9] log a transaction */ NULL, /* [#3] header parser */ init_child, /* child_init */ NULL, /* child_exit */ NULL /* [#0] post read-request */}; /* the cache */static cache *cachep; /* whether proxy module is available or not */static int proxy_available; /* the txt mapfile parsing stuff */static regex_t *lookup_map_txtfile_regexp = NULL;static regmatch_t lookup_map_txtfile_regmatch[MAX_NMATCH];/*** +-------------------------------------------------------+** | |** | configuration directive handling** | |** +-------------------------------------------------------+*//***** per-server configuration structure handling***/static void *config_server_create(pool *p, server_rec *s){ rewrite_server_conf *a; a = (rewrite_server_conf *)ap_pcalloc(p, sizeof(rewrite_server_conf)); a->state = ENGINE_DISABLED; a->options = OPTION_NONE; a->rewritelogfile = NULL; a->rewritelogfp = -1; a->rewriteloglevel = 0; a->rewritelockfile = NULL; a->rewritelockfp = -1; a->rewritemaps = ap_make_array(p, 2, sizeof(rewritemap_entry)); a->rewriteconds = ap_make_array(p, 2, sizeof(rewritecond_entry)); a->rewriterules = ap_make_array(p, 2, sizeof(rewriterule_entry)); a->server = s; return (void *)a;}static void *config_server_merge(pool *p, void *basev, void *overridesv){ rewrite_server_conf *a, *base, *overrides; a = (rewrite_server_conf *)ap_pcalloc(p, sizeof(rewrite_server_conf)); base = (rewrite_server_conf *)basev; overrides = (rewrite_server_conf *)overridesv; a->state = overrides->state; a->options = overrides->options; a->server = overrides->server; if (a->options & OPTION_INHERIT) { /* * local directives override * and anything else is inherited */ a->rewriteloglevel = overrides->rewriteloglevel != 0 ? overrides->rewriteloglevel : base->rewriteloglevel; a->rewritelogfile = overrides->rewritelogfile != NULL ? overrides->rewritelogfile : base->rewritelogfile; a->rewritelogfp = overrides->rewritelogfp != -1 ? overrides->rewritelogfp : base->rewritelogfp; a->rewritelockfile = overrides->rewritelockfile != NULL ? overrides->rewritelockfile : base->rewritelockfile; a->rewritelockfp = overrides->rewritelockfp != -1 ? overrides->rewritelockfp : base->rewritelockfp; a->rewritemaps = ap_append_arrays(p, overrides->rewritemaps, base->rewritemaps); a->rewriteconds = ap_append_arrays(p, overrides->rewriteconds, base->rewriteconds); a->rewriterules = ap_append_arrays(p, overrides->rewriterules, base->rewriterules); } else { /* * local directives override * and anything else gets defaults */ a->rewriteloglevel = overrides->rewriteloglevel; a->rewritelogfile = overrides->rewritelogfile; a->rewritelogfp = overrides->rewritelogfp; a->rewritelockfile = overrides->rewritelockfile; a->rewritelockfp = overrides->rewritelockfp; a->rewritemaps = overrides->rewritemaps; a->rewriteconds = overrides->rewriteconds; a->rewriterules = overrides->rewriterules; } return (void *)a;}/***** per-directory configuration structure handling***/static void *config_perdir_create(pool *p, char *path){ rewrite_perdir_conf *a; a = (rewrite_perdir_conf *)ap_pcalloc(p, sizeof(rewrite_perdir_conf)); a->state = ENGINE_DISABLED; a->options = OPTION_NONE; a->baseurl = NULL; a->rewriteconds = ap_make_array(p, 2, sizeof(rewritecond_entry)); a->rewriterules = ap_make_array(p, 2, sizeof(rewriterule_entry)); if (path == NULL) { a->directory = NULL; } else { /* make sure it has a trailing slash */ if (path[strlen(path)-1] == '/') { a->directory = ap_pstrdup(p, path); } else { a->directory = ap_pstrcat(p, path, "/", NULL); } } return (void *)a;}static void *config_perdir_merge(pool *p, void *basev, void *overridesv){ rewrite_perdir_conf *a, *base, *overrides; a = (rewrite_perdir_conf *)ap_pcalloc(p, sizeof(rewrite_perdir_conf)); base = (rewrite_perdir_conf *)basev; overrides = (rewrite_perdir_conf *)overridesv; a->state = overrides->state; a->options = overrides->options; a->directory = overrides->directory; a->baseurl = overrides->baseurl; if (a->options & OPTION_INHERIT) { a->rewriteconds = ap_append_arrays(p, overrides->rewriteconds, base->rewriteconds); a->rewriterules = ap_append_arrays(p, overrides->rewriterules, base->rewriterules); } else { a->rewriteconds = overrides->rewriteconds; a->rewriterules = overrides->rewriterules; } return (void *)a;}/***** the configuration commands***/static const char *cmd_rewriteengine(cmd_parms *cmd, rewrite_perdir_conf *dconf, int flag){ rewrite_server_conf *sconf; sconf = (rewrite_server_conf *)ap_get_module_config(cmd->server->module_config, &rewrite_module); if (cmd->path == NULL) { /* is server command */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -