应用程序驻留在此处:http://localhost/reports/
我想根据以下情况编写.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] => /报告/ 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
答案 0 :(得分:0)
听起来你已经为case1编写了规则...但是当URL没有以“daily_stats”结束时你需要阻止规则应用。为此,只需在匹配字符串的末尾添加$
即可。这样的事情应该有效:
RewriteRule ^reports/daily_stats/?$ /reports/daily_stats.php