问题
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.
It works fine without the .htaccess file when I visit:
public/index.php/hello
I get:
Welcome! This is the main page.
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?
Simple routing script that echoes content based on the url data that trails the script
<?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);
?>
My .HTACCESS file
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]
Update #1
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
回答1:
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]
来源:https://stackoverflow.com/questions/22129977/serverpath-info-variable-failing-due-to-mod-rewrite