$ _SERVER ['PATH_INFO]变量由于mod_rewrite而失败 [英] $_SERVER['PATH_INFO] variable failing due to mod_rewrite
问题描述
我目前正在尝试建立一个小型路由系统,但是当我将public/index.php重写为public/我的$ _SERVER ['PATH_INFO]变量时,无法获得任何输入.
I'm currently trying to set up a little routing system but when I rewrite my public/index.php to just public/ my $_SERVER['PATH_INFO] variable fails to get any input.
当我访问.htaccess文件时,它工作正常:
It works fine without the .htaccess file when I visit:
public/index.php/hello
我得到:
欢迎光临!这是主页.
但是当我重写以删除index.php并只公开公开/我已经死在水里了.
But when I rewrite to remove index.php and only leave public/ I am dead in the water.
有人知道这个解决方法吗,或者有人可以给我一个解释吗?
Does anybody know a fix for this or can someone give me an explanation for it?
简单的路由脚本,该脚本根据跟踪脚本的URL数据回显内容
<?php
// First, let's define our list of routes.
// We could put this in a different file and include it in order to separate
// logic and configuration.
$routes = array(
'/' => 'Welcome! This is the main page.',
'/hello' => 'Hello, World!',
'/users' => 'Users!'
);
// This is our router.
function router($routes)
{
// Iterate through a given list of routes.
foreach ($routes as $path => $content) {
if ($path == $_SERVER['PATH_INFO']) {
// If the path matches, display its contents and stop the router.
echo $content;
return;
}
}
// This can only be reached if none of the routes matched the path.
echo 'Sorry! Page not found';
}
// Execute the router with our list of routes.
router($routes);
?>
我的.HTACCESS文件
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]
更新#1
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
推荐答案
您没有规则告诉诸如/public/hello
之类的请求被路由到index.php文件. Apache和PATH INFO不够聪明,无法弄清楚.您需要明确告诉apache您要将请求路由到脚本:
You don't have a rule that tells requests like /public/hello
to be routed to the index.php file. Apache and PATH INFO isn't smart enough to figure that out. You need to explicitly tell apache that you want a request routed to a script:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^public/(.*)$ /public/index.php/$1 [L]
这篇关于$ _SERVER ['PATH_INFO]变量由于mod_rewrite而失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!