📄 mod_rewrite.c
字号:
r->status = HTTP_OK; /* make Apache kernel happy */ } else { n = REDIRECT; } /* now do the redirection */ ap_table_setn(r->headers_out, "Location", r->filename); rewritelog(r, 1, "redirect to %s [REDIRECT/%d]", r->filename, n); return n; } else if (strlen(r->filename) > 10 && strncmp(r->filename, "forbidden:", 10) == 0) { /* This URLs is forced to be forbidden for the requester */ return FORBIDDEN; } else if (strlen(r->filename) > 5 && strncmp(r->filename, "gone:", 5) == 0) { /* This URLs is forced to be gone */ return HTTP_GONE; } else if (strlen(r->filename) > 12 && strncmp(r->filename, "passthrough:", 12) == 0) { /* * Hack because of underpowered API: passing the current * rewritten filename through to other URL-to-filename handlers * just as it were the requested URL. This is to enable * post-processing by mod_alias, etc. which always act on * r->uri! The difference here is: We do not try to * add the document root */ r->uri = ap_pstrdup(r->pool, r->filename+12); return DECLINED; } else { /* it was finally rewritten to a local path */ /* expand "/~user" prefix */#ifndef WIN32 r->filename = expand_tildepaths(r, r->filename);#endif rewritelog(r, 2, "local path result: %s", r->filename); /* the filename has to start with a slash! */ if (r->filename[0] != '/') { return BAD_REQUEST; } /* if there is no valid prefix, we have * to emulate the translator from the core and * prefix the filename with document_root * * NOTICE: * We cannot leave out the prefix_stat because * - when we always prefix with document_root * then no absolute path can be created, e.g. via * emulating a ScriptAlias directive, etc. * - when we always NOT prefix with document_root * then the files under document_root have to * be references directly and document_root * gets never used and will be a dummy parameter - * this is also bad * * BUT: * Under real Unix systems this is no problem, * because we only do stat() on the first directory * and this gets cached by the kernel for along time! */ n = prefix_stat(r->filename, &finfo); if (n == 0) { if ((ccp = ap_document_root(r)) != NULL) { l = ap_cpystrn(docroot, ccp, sizeof(docroot)) - docroot; /* always NOT have a trailing slash */ if (docroot[l-1] == '/') { docroot[l-1] = '\0'; } if (r->server->path && !strncmp(r->filename, r->server->path, r->server->pathlen)) { r->filename = ap_pstrcat(r->pool, docroot, (r->filename + r->server->pathlen), NULL); } else { r->filename = ap_pstrcat(r->pool, docroot, r->filename, NULL); } rewritelog(r, 2, "prefixed with document_root to %s", r->filename); } } rewritelog(r, 1, "go-ahead with %s [OK]", r->filename); return OK; } } else { rewritelog(r, 1, "pass through %s", r->filename); return DECLINED; }}/***** MIME-type hook**** [used to support the forced-MIME-type feature]***/static int hook_mimetype(request_rec *r){ const char *t; /* now check if we have to force a MIME-type */ t = ap_table_get(r->notes, REWRITE_FORCED_MIMETYPE_NOTEVAR); if (t == NULL) { return DECLINED; } else { rewritelog(r, 1, "force filename %s to have MIME-type '%s'", r->filename, t); r->content_type = t; return OK; }}/***** Fixup hook**** [used for the rewriting engine triggered by** the per-directory 'RewriteRule' directives]***/static int hook_fixup(request_rec *r){ rewrite_perdir_conf *dconf; char *cp; char *cp2; const char *ccp; char *prefix; int l; int n; char *ofilename; dconf = (rewrite_perdir_conf *)ap_get_module_config(r->per_dir_config, &rewrite_module); /* if there is no per-dir config we return immediately */ if (dconf == NULL) { return DECLINED; } /* we shouldn't do anything in subrequests */ if (r->main != NULL) { return DECLINED; } /* if there are no real (i.e. no RewriteRule directives!) per-dir config of us, we return also immediately */ if (dconf->directory == NULL) { return DECLINED; } /* * only do something under runtime if the engine is really enabled, * for this directory, else return immediately! */ if (!(ap_allow_options(r) & (OPT_SYM_LINKS | OPT_SYM_OWNER))) { /* FollowSymLinks is mandatory! */ ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r, "Options FollowSymLinks or SymLinksIfOwnerMatch is off " "which implies that RewriteRule directive is forbidden: " "%s", r->filename); return FORBIDDEN; } else { /* FollowSymLinks is given, but the user can * still turn off the rewriting engine */ if (dconf->state == ENGINE_DISABLED) { return DECLINED; } } /* * remember the current filename before rewriting for later check * to prevent deadlooping because of internal redirects * on final URL/filename which can be equal to the inital one. */ ofilename = r->filename; /* * now apply the rules ... */ if (apply_rewrite_list(r, dconf->rewriterules, dconf->directory)) { if (strlen(r->filename) > 6 && strncmp(r->filename, "proxy:", 6) == 0) { /* it should go on as an internal proxy request */ /* make sure the QUERY_STRING and * PATH_INFO parts get incorporated * (r->path_info was already appended by the * rewriting engine because of the per-dir context!) */ if (r->args != NULL && r->uri == r->unparsed_uri) { /* see proxy_http:proxy_http_canon() */ r->filename = ap_pstrcat(r->pool, r->filename, "?", r->args, NULL); } /* now make sure the request gets handled by the proxy handler */ r->proxyreq = 1; r->handler = "proxy-server"; rewritelog(r, 1, "[per-dir %s] go-ahead with proxy request " "%s [OK]", dconf->directory, r->filename); return OK; } else if ( (strlen(r->filename) > 7 && strncmp(r->filename, "http://", 7) == 0) || (strlen(r->filename) > 8 && strncmp(r->filename, "https://", 8) == 0) || (strlen(r->filename) > 9 && strncmp(r->filename, "gopher://", 9) == 0) || (strlen(r->filename) > 6 && strncmp(r->filename, "ftp://", 6) == 0) ) { /* it was finally rewritten to a remote URL */ /* because we are in a per-dir context * first try to replace the directory with its base-URL * if there is a base-URL available */ if (dconf->baseurl != NULL) { /* skip 'scheme:' */ for (cp = r->filename; *cp != ':' && *cp != '\0'; cp++) ; /* skip '://' */ cp += 3; if ((cp = strchr(cp, '/')) != NULL) { rewritelog(r, 2, "[per-dir %s] trying to replace " "prefix %s with %s", dconf->directory, dconf->directory, dconf->baseurl); cp2 = subst_prefix_path(r, cp, dconf->directory, dconf->baseurl); if (strcmp(cp2, cp) != 0) { *cp = '\0'; r->filename = ap_pstrcat(r->pool, r->filename, cp2, NULL); } } } /* now prepare the redirect... */ /* skip 'scheme:' */ for (cp = r->filename; *cp != ':' && *cp != '\0'; cp++) ; /* skip '://' */ cp += 3; /* skip host part */ for ( ; *cp != '/' && *cp != '\0'; cp++) ; if (*cp != '\0') { rewritelog(r, 1, "[per-dir %s] escaping %s for redirect", dconf->directory, r->filename); cp2 = escape_uri(r->pool, cp); *cp = '\0'; r->filename = ap_pstrcat(r->pool, r->filename, cp2, NULL); } /* append the QUERY_STRING part */ if (r->args != NULL) { r->filename = ap_pstrcat(r->pool, r->filename, "?", r->args, NULL); } /* determine HTTP redirect response code */ if (ap_is_HTTP_REDIRECT(r->status)) { n = r->status; r->status = HTTP_OK; /* make Apache kernel happy */ } else { n = REDIRECT; } /* now do the redirection */ ap_table_setn(r->headers_out, "Location", r->filename); rewritelog(r, 1, "[per-dir %s] redirect to %s [REDIRECT/%d]", dconf->directory, r->filename, n); return n; } else if (strlen(r->filename) > 10 && strncmp(r->filename, "forbidden:", 10) == 0) { /* This URL is forced to be forbidden for the requester */ return FORBIDDEN; } else if (strlen(r->filename) > 5 && strncmp(r->filename, "gone:", 5) == 0) { /* This URL is forced to be gone */ return HTTP_GONE; } else { /* it was finally rewritten to a local path */ /* if someone used the PASSTHROUGH flag in per-dir * context we just ignore it. It is only useful * in per-server context */ if (strlen(r->filename) > 12 && strncmp(r->filename, "passthrough:", 12) == 0) { r->filename = ap_pstrdup(r->pool, r->filename+12); } /* the filename has to start with a slash! */ if (r->filename[0] != '/') { return BAD_REQUEST; } /* Check for deadlooping: * At this point we KNOW that at least one rewriting * rule was applied, but when the resulting URL is * the same as the initial URL, we are not allowed to * use the following internal redirection stuff because * this would lead to a deadloop. */ if (strcmp(r->filename, ofilename) == 0) { rewritelog(r, 1, "[per-dir %s] initial URL equal rewritten " "URL: %s [IGNORING REWRITE]", dconf->directory, r->filename); return OK; } /* if there is a valid base-URL then substitute * the per-dir prefix with this base-URL if the * current filename still is inside this per-dir * context. If not then treat the result as a * plain URL */ if (dconf->baseurl != NULL) { rewritelog(r, 2, "[per-dir %s] trying to replace prefix %s with %s", dconf->directory, dconf->directory, dconf->baseurl); r->filename = subst_prefix_path(r, r->filename, dconf->directory, dconf->baseurl); } else { /* if no explicit base-URL exists we assume * that the directory prefix is also a valid URL * for this webserver and only try to remove the * document_root if it is prefix */ if ((ccp = ap_document_root(r)) != NULL) { prefix = ap_pstrdup(r->pool, ccp); /* always NOT have a trailing slash */ l = strlen(prefix); if (prefix[l-1] == '/') { prefix[l-1] = '\0'; l--; } if (strncmp(r->filename, prefix, l) == 0) { rewritelog(r, 2, "[per-dir %s] strip document_root " "prefix: %s -> %s", dconf->directory, r->filename, r->filename+l); r->filename = ap_pstrdup(r->pool, r->filename+l); } } } /* now initiate the internal redirect */ rewritelog(r, 1, "[per-dir %s] internal redirect with %s " "[INTERNAL REDIRECT]", dconf->directory, r->filename); r->filename = ap_pstrcat(r->pool, "redirect:", r->filename, NULL); r->handler = "redirect-handler"; return OK;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -