为什么 mod_rewrite 不重新处理重写的路径?

为什么 mod_rewrite 不重新处理重写的路径?

我正在重写路径以设置环境变量,这将导致 mod_jk 使用特定的 JBoss 服务器。可与任何应用程序路径完美配合。

当我尝试我们的监控servelet(执行简单的数据库来证明堆栈可以运行)时,它匹配重写,执行重写,设置环境变量,但随后第二次实际处理URL时失败。

为什么第二次通过重写引擎时的行为不一致?

模式是,如果/workername/添加到路径前面,它将被删除但使用该值作为JK_WORKER_NAMEmod_jk 挂载。

示例配置:

SetHandler jakarta-servlet  
SetEnv JK_WORKER_NAME mexico

RewriteEngine on
RewriteRule ^/(mexico[1-2])/(.*)$ /$2  [E=JK_WORKER_NAME:$1,L] 

(mod_jk 工作者被命名为 mexico1 和 mexico2)

命中后https://hostname/mexico2/public/gateway.sf进行重写,然后第二次通过重写引擎,由 mod_JK 处理并传递到应用服务器。一切顺利,查看日志(向右滚动查看精彩内容)

10.1.1.163 - - [26/Aug/2013:14:10:44 --0400] [publisher.internal.test.hnw.com/sid#2ad8c96bc310][rid#2ad8bde6fae0/initial] (2) init rewrite engine with requested uri /mexico2/public/gateway.sf
10.1.1.163 - - [26/Aug/2013:14:10:44 --0400] [publisher.internal.test.hnw.com/sid#2ad8c96bc310][rid#2ad8bde6fae0/initial] (3) applying pattern '.*' to uri '/mexico2/public/gateway.sf'
10.1.1.163 - - [26/Aug/2013:14:10:44 --0400] [publisher.internal.test.hnw.com/sid#2ad8c96bc310][rid#2ad8bde6fae0/initial] (4) RewriteCond: input='GET' pattern='^TRACE' => not-matched
10.1.1.163 - - [26/Aug/2013:14:10:44 --0400] [publisher.internal.test.hnw.com/sid#2ad8c96bc310][rid#2ad8bde6fae0/initial] (3) applying pattern '^/(mexico[1-2])/(.*)$' to uri '/mexico2/public/gateway.sf'
10.1.1.163 - - [26/Aug/2013:14:10:44 --0400] [publisher.internal.test.hnw.com/sid#2ad8c96bc310][rid#2ad8bde6fae0/initial] (2) rewrite '/mexico2/public/gateway.sf' -> '/public/gateway.sf'
10.1.1.163 - - [26/Aug/2013:14:10:44 --0400] [publisher.internal.test.hnw.com/sid#2ad8c96bc310][rid#2ad8bde6fae0/initial] (2) local path result: /public/gateway.sf
10.1.1.163 - - [26/Aug/2013:14:10:44 --0400] [publisher.internal.test.hnw.com/sid#2ad8c96bc310][rid#2ad8bde6fae0/initial] (2) prefixed with document_root to /data/publisher/webdav/images/notice/public/gateway.sf
10.1.1.163 - - [26/Aug/2013:14:10:44 --0400] [publisher.internal.test.hnw.com/sid#2ad8c96bc310][rid#2ad8bde6fae0/initial] (1) go-ahead with /data/publisher/webdav/images/notice/public/gateway.sf [OK]
10.1.1.163 - - [26/Aug/2013:14:10:44 --0400] [publisher.internal.test.hnw.com/sid#2ad8c96bc310][rid#2ad8bde84180/initial] (2) init rewrite engine with requested uri /public/gateway.sf
10.1.1.163 - - [26/Aug/2013:14:10:44 --0400] [publisher.internal.test.hnw.com/sid#2ad8c96bc310][rid#2ad8bde84180/initial] (3) applying pattern '.*' to uri '/public/gateway.sf'
10.1.1.163 - - [26/Aug/2013:14:10:44 --0400] [publisher.internal.test.hnw.com/sid#2ad8c96bc310][rid#2ad8bde84180/initial] (4) RewriteCond: input='GET' pattern='^TRACE' => not-matched
10.1.1.163 - - [26/Aug/2013:14:10:44 --0400] [publisher.internal.test.hnw.com/sid#2ad8c96bc310][rid#2ad8bde84180/initial] (3) applying pattern '^/(mexico[1-2])/(.*)$' to uri '/public/gateway.sf'
10.1.1.163 - - [26/Aug/2013:14:10:44 --0400] [publisher.internal.test.hnw.com/sid#2ad8c96bc310][rid#2ad8bde84180/initial] (1) pass through /public/gateway.sf

命中https://hostname/mexico1/monitorme.mmm也符合条件并被重写,但之后再也没有通过引擎,Apache 尝试从文档根目录为该路径提供服务。所有命中都传递到 JBoss,文档根目录中没有文档。因此我得到一个页面未找到。

10.1.1.163 - - [26/Aug/2013:14:12:17 --0400] [publisher.internal.test.hnw.com/sid#2ad8c96bc310][rid#2ad8bde71af0/initial] (2) init rewrite engine with requested uri /mexico1/monitorme.mmm
10.1.1.163 - - [26/Aug/2013:14:12:17 --0400] [publisher.internal.test.hnw.com/sid#2ad8c96bc310][rid#2ad8bde71af0/initial] (3) applying pattern '.*' to uri '/mexico1/monitorme.mmm'
10.1.1.163 - - [26/Aug/2013:14:12:17 --0400] [publisher.internal.test.hnw.com/sid#2ad8c96bc310][rid#2ad8bde71af0/initial] (4) RewriteCond: input='GET' pattern='^TRACE' => not-matched
10.1.1.163 - - [26/Aug/2013:14:12:17 --0400] [publisher.internal.test.hnw.com/sid#2ad8c96bc310][rid#2ad8bde71af0/initial] (3) applying pattern '^/(mexico[1-2])/(.*)$' to uri '/mexico1/monitorme.mmm'
10.1.1.163 - - [26/Aug/2013:14:12:17 --0400] [publisher.internal.test.hnw.com/sid#2ad8c96bc310][rid#2ad8bde71af0/initial] (2) rewrite '/mexico1/monitorme.mmm' -> '/monitorme.mmm'
10.1.1.163 - - [26/Aug/2013:14:12:17 --0400] [publisher.internal.test.hnw.com/sid#2ad8c96bc310][rid#2ad8bde71af0/initial] (2) local path result: /monitorme.mmm
10.1.1.163 - - [26/Aug/2013:14:12:17 --0400] [publisher.internal.test.hnw.com/sid#2ad8c96bc310][rid#2ad8bde71af0/initial] (2) prefixed with document_root to /data/publisher/webdav/images/notice/monitorme.mmm
10.1.1.163 - - [26/Aug/2013:14:12:17 --0400] [publisher.internal.test.hnw.com/sid#2ad8c96bc310][rid#2ad8bde71af0/initial] (1) go-ahead with /data/publisher/webdav/images/notice/monitorme.mmm [OK]

答案1

如果您使用上面的示例,第一次运行就会成功,这有点令人惊讶RewriteRule。我以为L应该在命中时停止执行。我认为您应该使用N而不是L

我没有尝试过并且不能称自己为mod_rewrite专家。:-)

last|L : Stop the rewriting process immediately and don't apply any more rules. Especially note caveats for per-directory and .htaccess context (see also the END flag).

next|N : Re-run the rewriting process, starting again with the first rule, using the result of the ruleset so far as a starting point.

相关内容