0

应用程序驻留在此处:http://localhost/reports/

  • 报告
    • 索引.php
    • daily_stats.php
    • 控制器
      • daily_stats.php //是一个以 br() 作为动作的控制器

我想根据以下情况编写一个 .htaccess 规则:

case1: http://localhost/reports/daily_stats/  
           resulting url: http://localhost/reports/daily_stats.php

case2: http://localhost/reports/daily_stats/br/  
           resulting url: http://localhost/reports/index.php/daily_stats/br

index.php 负责路由到控制器的位置

我已经写了以下内容:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /reports/index.php/$1 

面临的问题是当我进入http://localhost/reports/daily_stats/br/ 它时执行根文件夹中的daily_stats.php 而不是控制器

当它看到服务器变量时,它是这样的:

[SCRIPT_FILENAME] => /var/www/reports/daily_stats.php
[QUERY_STRING] => 
[REQUEST_URI] => /reports/daily_stats/br
[SCRIPT_NAME] => /reports/daily_stats.php
[PATH_INFO] => /br
[PATH_TRANSLATED] => /var/www/br
[PHP_SELF] => /reports/daily_stats.php/br

应该是这样的: [SCRIPT_FILENAME] => /var/www/reports/index.php [QUERY_STRING] => [REQUEST_URI] => /reports/daily_stats/br [SCRIPT_NAME] => /reports/index.php [PATH_INFO] => /daily_stats/br [PATH_TRANSLATED] => /var/www/daily_stats/br [PHP_SELF] => /reports/index.php/daily_stats/br

重写日志说:

127.0.0.1 - - [07/Aug/2012:15:22:30 +051800] [localhost/sid#b63fead8][rid#b6ae6058/subreq] (3) [perdir /var/www/reports/] add path info postfix: /var/www/reports/daily_stats.php -> /var/www/reports/daily_stats.php/br
127.0.0.1 - - [07/Aug/2012:15:22:30 +051800] [localhost/sid#b63fead8][rid#b6ae6058/subreq] (3) [perdir /var/www/reports/] strip per-dir prefix: /var/www/reports/daily_stats.php/br -> daily_stats.php/br
127.0.0.1 - - [07/Aug/2012:15:22:30 +051800] [localhost/sid#b63fead8][rid#b6ae6058/subreq] (3) [perdir /var/www/reports/] applying pattern '^(.*)$' to uri 'daily_stats.php/br'
127.0.0.1 - - [07/Aug/2012:15:22:30 +051800] [localhost/sid#b63fead8][rid#b6ae6058/subreq] (4) [perdir /var/www/reports/] RewriteCond: input='/var/www/reports/daily_stats.php' pattern='!-f' => not-matched
127.0.0.1 - - [07/Aug/2012:15:22:30 +051800] [localhost/sid#b63fead8][rid#b6ae6058/subreq] (1) [perdir /var/www/reports/] pass through /var/www/reports/daily_stats.php
127.0.0.1 - - [07/Aug/2012:15:22:30 +051800] [localhost/sid#b63fead8][rid#b6af0058/initial] (3) [perdir /var/www/reports/] add path info postfix: /var/www/reports/daily_stats.php -> /var/www/reports/daily_stats.php/br
127.0.0.1 - - [07/Aug/2012:15:22:30 +051800] [localhost/sid#b63fead8][rid#b6af0058/initial] (3) [perdir /var/www/reports/] strip per-dir prefix: /var/www/reports/daily_stats.php/br -> daily_stats.php/br
127.0.0.1 - - [07/Aug/2012:15:22:30 +051800] [localhost/sid#b63fead8][rid#b6af0058/initial] (3) [perdir /var/www/reports/] applying pattern '^(.*)$' to uri 'daily_stats.php/br'
127.0.0.1 - - [07/Aug/2012:15:22:30 +051800] [localhost/sid#b63fead8][rid#b6af0058/initial] (4) [perdir /var/www/reports/] RewriteCond: input='/var/www/reports/daily_stats.php' pattern='!-f' => not-matched
127.0.0.1 - - [07/Aug/2012:15:22:30 +051800] [localhost/sid#b63fead8][rid#b6af0058/initial] (1) [perdir /var/www/reports/] pass through /var/www/reports/daily_stats.php
4

1 回答 1

0

听起来您已经为 case1 编写了规则……但是当 URL 未以“daily_stats”结尾时,您需要阻止规则应用。为此,只需$在匹配字符串的末尾添加一个。像这样的东西应该工作:

RewriteRule ^reports/daily_stats/?$ /reports/daily_stats.php
于 2012-10-19T14:06:17.380 回答