mod_rewrite使用PATH_INFO旋转关闭重写的附加子请求

时间:2015-02-03 01:18:18

标签: apache .htaccess mod-rewrite

假设我想将以/test/开头的所有请求路由到单个PHP文件info.php。我可以通过将原始%{REQUEST_URI}传递为:

来完成此操作
  1. 查询字符串参数,如

    info.php?request_uri=/test/some/path
    

  2. PATH_INFO - 紧跟info.php之后的路径字符串,如

    info.php/test/some/path
    
  3. 还有其他方法(例如环境变量),但我的问题是关于使用PATH_INFO时的奇怪行为。

    问题在于,根据重写日志,PATH_INFO方法比查询字符串方法再生成一个子请求。

    有人可以解释为什么会这样吗?

    详细

    DOCUMENT_ROOT位于/var/wwwhttpd -V的第一行是:

    Server version: Apache/2.2.15 (Unix)
    

    PATH_INFO

    /var/www/test/.htaccess文件的内容如下:

    DirectoryIndex disabled
    DirectorySlash off
    Options +FollowSymLinks -MultiViews
    
    RewriteEngine on
    
    # Only perform the rewrite for first round of mod_rewrite processing.
    RewriteCond %{ENV:REDIRECT_STATUS} =""
    # Pass current path as PATH_INFO to PHP script.
    RewriteRule ^ info.php%{REQUEST_URI}
    

    /test/some/path的请求产生以下重写输出:

    127.0.0.1 - - [02/Feb/2015:19:46:22 --0500] [localhost/sid#7f5a056a9880][rid#7f5a05b5d888/initial] (3) [perdir /var/www/test/] add path info postfix: /var/www/test/some -> /var/www/test/some/path
    127.0.0.1 - - [02/Feb/2015:19:46:22 --0500] [localhost/sid#7f5a056a9880][rid#7f5a05b5d888/initial] (3) [perdir /var/www/test/] strip per-dir prefix: /var/www/test/some/path -> some/path
    127.0.0.1 - - [02/Feb/2015:19:46:22 --0500] [localhost/sid#7f5a056a9880][rid#7f5a05b5d888/initial] (3) [perdir /var/www/test/] applying pattern '^' to uri 'some/path'
    127.0.0.1 - - [02/Feb/2015:19:46:22 --0500] [localhost/sid#7f5a056a9880][rid#7f5a05b5d888/initial] (4) [perdir /var/www/test/] RewriteCond: input='' pattern='=' => matched
    127.0.0.1 - - [02/Feb/2015:19:46:22 --0500] [localhost/sid#7f5a056a9880][rid#7f5a05b5d888/initial] (2) [perdir /var/www/test/] rewrite 'some/path' -> 'info.php/test/some/path'
    127.0.0.1 - - [02/Feb/2015:19:46:22 --0500] [localhost/sid#7f5a056a9880][rid#7f5a05b5d888/initial] (3) [perdir /var/www/test/] add per-dir prefix: info.php/test/some/path -> /var/www/test/info.php/test/some/path
    127.0.0.1 - - [02/Feb/2015:19:46:22 --0500] [localhost/sid#7f5a056a9880][rid#7f5a05b5d888/initial] (2) [perdir /var/www/test/] strip document_root prefix: /var/www/test/info.php/test/some/path -> /test/info.php/test/some/path
    127.0.0.1 - - [02/Feb/2015:19:46:22 --0500] [localhost/sid#7f5a056a9880][rid#7f5a05b5d888/initial] (1) [perdir /var/www/test/] internal redirect with /test/info.php/test/some/path [INTERNAL REDIRECT]
    127.0.0.1 - - [02/Feb/2015:19:46:22 --0500] [localhost/sid#7f5a056a9880][rid#7f5a05b54fd0/initial/redir#1] (3) [perdir /var/www/test/] add path info postfix: /var/www/test/info.php -> /var/www/test/info.php/test/some/path
    127.0.0.1 - - [02/Feb/2015:19:46:22 --0500] [localhost/sid#7f5a056a9880][rid#7f5a05b54fd0/initial/redir#1] (3) [perdir /var/www/test/] strip per-dir prefix: /var/www/test/info.php/test/some/path -> info.php/test/some/path
    127.0.0.1 - - [02/Feb/2015:19:46:22 --0500] [localhost/sid#7f5a056a9880][rid#7f5a05b54fd0/initial/redir#1] (3) [perdir /var/www/test/] applying pattern '^' to uri 'info.php/test/some/path'
    127.0.0.1 - - [02/Feb/2015:19:46:22 --0500] [localhost/sid#7f5a056a9880][rid#7f5a05b54fd0/initial/redir#1] (4) [perdir /var/www/test/] RewriteCond: input='200' pattern='=' => not-matched
    127.0.0.1 - - [02/Feb/2015:19:46:22 --0500] [localhost/sid#7f5a056a9880][rid#7f5a05b54fd0/initial/redir#1] (1) [perdir /var/www/test/] pass through /var/www/test/info.php
    
    (The subrequest begins here)
    
    127.0.0.1 - - [02/Feb/2015:19:46:22 --0500] [localhost/sid#7f5a056a9880][rid#7f5a05b4d818/subreq] (3) [perdir /var/www/test/] add path info postfix: /var/www/test/some -> /var/www/test/some/path
    127.0.0.1 - - [02/Feb/2015:19:46:22 --0500] [localhost/sid#7f5a056a9880][rid#7f5a05b4d818/subreq] (3) [perdir /var/www/test/] strip per-dir prefix: /var/www/test/some/path -> some/path
    127.0.0.1 - - [02/Feb/2015:19:46:22 --0500] [localhost/sid#7f5a056a9880][rid#7f5a05b4d818/subreq] (3) [perdir /var/www/test/] applying pattern '^' to uri 'some/path'
    127.0.0.1 - - [02/Feb/2015:19:46:22 --0500] [localhost/sid#7f5a056a9880][rid#7f5a05b4d818/subreq] (4) [perdir /var/www/test/] RewriteCond: input='200' pattern='=' => not-matched
    127.0.0.1 - - [02/Feb/2015:19:46:22 --0500] [localhost/sid#7f5a056a9880][rid#7f5a05b4d818/subreq] (1) [perdir /var/www/test/] pass through /var/www/test/some
    

    查询字符串

    /var/www/test/.htaccess的最后一行更改为:

    RewriteRule ^ info.php?request_uri=%{REQUEST_URI}
    

    /test/some/path的请求产生以下重写输出:

    127.0.0.1 - - [02/Feb/2015:19:49:07 --0500] [localhost/sid#7f5a056a9880][rid#7f5a05b56858/initial] (3) [perdir /var/www/test/] add path info postfix: /var/www/test/some -> /var/www/test/some/path
    127.0.0.1 - - [02/Feb/2015:19:49:07 --0500] [localhost/sid#7f5a056a9880][rid#7f5a05b56858/initial] (3) [perdir /var/www/test/] strip per-dir prefix: /var/www/test/some/path -> some/path
    127.0.0.1 - - [02/Feb/2015:19:49:07 --0500] [localhost/sid#7f5a056a9880][rid#7f5a05b56858/initial] (3) [perdir /var/www/test/] applying pattern '^' to uri 'some/path'
    127.0.0.1 - - [02/Feb/2015:19:49:07 --0500] [localhost/sid#7f5a056a9880][rid#7f5a05b56858/initial] (4) [perdir /var/www/test/] RewriteCond: input='' pattern='=' => matched
    127.0.0.1 - - [02/Feb/2015:19:49:07 --0500] [localhost/sid#7f5a056a9880][rid#7f5a05b56858/initial] (2) [perdir /var/www/test/] rewrite 'some/path' -> 'info.php?request_uri=/test/some/path'
    127.0.0.1 - - [02/Feb/2015:19:49:07 --0500] [localhost/sid#7f5a056a9880][rid#7f5a05b56858/initial] (3) split uri=info.php?request_uri=/test/some/path -> uri=info.php, args=request_uri=/test/some/path
    127.0.0.1 - - [02/Feb/2015:19:49:07 --0500] [localhost/sid#7f5a056a9880][rid#7f5a05b56858/initial] (3) [perdir /var/www/test/] add per-dir prefix: info.php -> /var/www/test/info.php
    127.0.0.1 - - [02/Feb/2015:19:49:07 --0500] [localhost/sid#7f5a056a9880][rid#7f5a05b56858/initial] (2) [perdir /var/www/test/] strip document_root prefix: /var/www/test/info.php -> /test/info.php
    127.0.0.1 - - [02/Feb/2015:19:49:07 --0500] [localhost/sid#7f5a056a9880][rid#7f5a05b56858/initial] (1) [perdir /var/www/test/] internal redirect with /test/info.php [INTERNAL REDIRECT]
    127.0.0.1 - - [02/Feb/2015:19:49:07 --0500] [localhost/sid#7f5a056a9880][rid#7f5a05b54fe0/initial/redir#1] (3) [perdir /var/www/test/] strip per-dir prefix: /var/www/test/info.php -> info.php
    127.0.0.1 - - [02/Feb/2015:19:49:07 --0500] [localhost/sid#7f5a056a9880][rid#7f5a05b54fe0/initial/redir#1] (3) [perdir /var/www/test/] applying pattern '^' to uri 'info.php'
    127.0.0.1 - - [02/Feb/2015:19:49:07 --0500] [localhost/sid#7f5a056a9880][rid#7f5a05b54fe0/initial/redir#1] (4) [perdir /var/www/test/] RewriteCond: input='200' pattern='=' => not-matched
    127.0.0.1 - - [02/Feb/2015:19:49:07 --0500] [localhost/sid#7f5a056a9880][rid#7f5a05b54fe0/initial/redir#1] (1) [perdir /var/www/test/] pass through /var/www/test/info.php
    

    如您所见,此重写输出中不会出现子请求。

0 个答案:

没有答案